-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to improve CLI parsing #8
Conversation
Neat, thanks! :) I probably won't have time to look at it in detail before January 1st, though. One minor thing I can see immediately: the commit message should begin with an uppercase letter after the module tag. I.e. "COMMON: Add a new CLI class" instead of "COMMON: add a new CLI class". |
commit messages are now fix |
ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really sorry, but didn't find any time for this earlier.
In principle, I like your implementation, good work! :)
There are a lot of style-issues, though, most prominently a lot of trailing whitespace (in more than just the places I marked). And the Callbacks leak. It's nothing major, but if you could fix those, that would be great.
I'm also not too sure I like the --help text output that much. In the manual ones, I introduced some groupings via newlines and I also hand-wrapped a few strings that overflowed 80 characters. If you know of a neat way to do that for your automatic text as well, I'd appreciate it.
src/common/cli.cpp
Outdated
helpStr += " "; | ||
helpStr += optionArgs; | ||
for (int i = (maxArgLength - strlen(longName) - strlen(optionArgs) - 4); i > 0; --i) { | ||
helpStr += " "; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/common/cli.cpp
Outdated
_val.insert(args[j]); | ||
return j - i; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/common/cli.cpp
Outdated
} | ||
} | ||
|
||
void Parser::addOption(const char *longName, char shortName, const char *help, OptionRet ret, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/common/cli.cpp
Outdated
fail: | ||
_returnVal = 1; | ||
this->printUsage(_helpStr); | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/common/cli.h
Outdated
_print(aPrinter), | ||
_printerStr(str) {} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
NoOption inFileOpt(false, new ValGetter<Common::UString &>(inFile, "input files")); | ||
NoOption outFileOpt(true, new ValGetter<Common::UString &>(outFile, "output files")); | ||
Parser parser(argv[0], "BioWare TLK to XML converter", | ||
"If no output file is given, the output is written to stdout.\n\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a \n at the start of that string too, please, and for the explanatory texts in other tools too.
src/convert2da.cpp
Outdated
std::fprintf(stream, "column layout. They will be pasted together and printed as one GDA.\n\n"); | ||
std::fprintf(stream, "If no output file is given, the output is written to stdout.\n"); | ||
} | ||
// std::fprintf(stream, " -o <file> --output <file> Write the output to this file\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason you kept this?
src/fixpremiumgff.cpp
Outdated
"If no ID is given is given, it is guessed from the file name.", | ||
returnValue, makeEndArgs(&inFileOpt, &outFileOpt)); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
src/gff2xml.cpp
Outdated
parser.addOption("dragonage2", "Use Dragon Age II encodings", kContinueParsing, | ||
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDDragonAge2, game))); | ||
parser.addOption("encoding", "Override an encoding", kContinueParsing, | ||
new Callback<EncodingOverrides &>("str", parseEncodingOverride, encOverrides)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valgrind says that this line leaks, the new Callback
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDJade, game))); | ||
parser.addOption("pass", "Decryption password, if required, in hex notation", | ||
kContinueParsing, | ||
new Callback<std::vector<byte> &>("hex", parsePassword, password)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too the Callback (and in the lines below) leak
e543567
to
bb16123
Compare
The goal of this new class is to simplifie the arguments parsing, I've take inspiration from glib Commandline option parser: https://developer.gnome.org/glib/stable/glib-Commandline-option-parser.html, exept that with glib you need to specifie with a flag the type of each options, here options type are automatically determine when adding an option with parser.addOption. Basically most of the job is made in the parser contructor, here you can set the name of the program, summary, and arguments that are not options. Option like "--nwm" are set with addOption function. "--help" is automatically generated with arguments you've use in addOption and in the contructor, "--version" is generated too, and call Version::printVersion.
In this file, "Usage: xml2ssf [<options>] [<input file>] <output file>" change to "Usage: ./bin/xml2ssf [<options>] <[input file] <output file>>". I had to use an array of stings to get input file and output file, this is because input file is the optinal argument and is before ouput file, I would be harder to handle this case automatically in cli.h, as I would need to check if there is non optinal argument after the optinal one. I could do that, but as this file is the only one that need that, I don't think it worth the effort so I just get all arguments in a vector, and let some bytes of the old parser do what's left of the job
Signed-off-by: Matthias Gatto <uso.cosmo.ray@gmail.com>
thanks for the review and sorry for the time I take to answer. |
Very nice, thank you! :) I merged it with this push: f0dc347...1b6d66a. |
Try to fix this:
https://wiki.xoreos.org/index.php?title=TODO#Command_line_parsing_in_the_tools
I've try to keep backward compatibility, so the usage should be just the same as before, I was unable to test every options of every program, but the one I've try was working, I hope I've break nothing.