clipp
clipp copied to clipboard
Issue with optional flag
See this example:
int main(int argc, char **argv) {
bool optionSpecified = false;
std::string optionvalue;
auto helpCmd = command("help");
auto cmd1 = command("cmd1");
auto cmd2 = command("cmd2");
auto cmd3 = command("cmd3");
auto optionFlag = (option("-o").set(optionSpecified, true) & value("optionvalue").set(optionvalue));
auto cli = (helpCmd | cmd1 | ((cmd2 | cmd3), optionFlag));
if (parse(argc, argv, cli)) {
tfm::printf("Option: %s Value: %s\n", optionSpecified, optionvalue);
} else {
tfm::printf("Parse failed!\n");
std::cerr << usage_lines(cli, argv[0]) << std::endl;
}
return 0;
}
This should accept the "help", "cmd1", "cmd2" or "cmd3" commands, but allow the "-o value" flag to be specified optionally on the "cmd2" or "cmd3" commands. However, while the usage_lines output seems to be correct, the parse fails if "cmd2" or "cmd3" is specified at all, with or without the "-o value" flag.
(tfm::printf is from https://github.com/c42f/tinyformat)
Am I doing something stupid or is this a bug?
You did everything right. It is a bug. I've just discovered where it goes wrong. You can use this workaround, until I have a fix ready:
auto dummy = option("");
auto cli = (helpCmd | (cmd1, dummy) | ((cmd2 | cmd3), optionFlag));
The dummy option doesn't show up in the usage lines.