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 |
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. |
helpStr += " "; | ||
helpStr += optionArgs; | ||
for (int i = (maxArgLength - strlen(longName) - strlen(optionArgs) - 4); i > 0; --i) { | ||
helpStr += " "; |
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
Trailing whitespace
_val.insert(args[j]); | ||
return j - i; | ||
} | ||
|
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
Trailing whitespace
} | ||
} | ||
|
||
void Parser::addOption(const char *longName, char shortName, const char *help, OptionRet ret, |
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
Trailing whitespace
fail: | ||
_returnVal = 1; | ||
this->printUsage(_helpStr); | ||
return false; |
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
Trailing whitespace
_print(aPrinter), | ||
_printerStr(str) {} | ||
|
||
|
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
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" |
DrMcCoy
Feb 24, 2017
Member
Add a \n at the start of that string too, please, and for the explanatory texts in other tools too.
Add a \n at the start of that string too, please, and for the explanatory texts in other tools too.
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"); |
DrMcCoy
Feb 24, 2017
Member
Any reason you kept this?
Any reason you kept this?
"If no ID is given is given, it is guessed from the file name.", | ||
returnValue, makeEndArgs(&inFileOpt, &outFileOpt)); | ||
|
||
|
DrMcCoy
Feb 24, 2017
Member
Trailing whitespace
Trailing whitespace
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)); |
DrMcCoy
Feb 24, 2017
Member
valgrind says that this line leaks, the new Callback
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)); |
DrMcCoy
Feb 24, 2017
Member
Here too the Callback (and in the lines below) leak
Here too the Callback (and in the lines below) leak
e543567
to
bb16123
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.