I'm getting familiar with boost's program options utility and I'm wondering is there a way of defining mutually exclusive option groups. I.e., my program has different flows:
program --first --opt1 --opt2 ...
program --second --opt3 --opt4 ...
So, my options for different flows are mutually exclusive. Is there a way to define mutually exclusive option groups?
Of course below code will do that:
/* Function used to check that 'opt1' and 'opt2' are not specified
at the same time. */
void conflicting_options(const variables_map& vm,
const char* opt1, const char* opt2)
{
if (vm.count(opt1) && !vm[opt1].defaulted()
&& vm.count(opt2) && !vm[opt2].defaulted())
throw logic_error(string("Conflicting options '")
+ opt1 + "' and '" + opt2 + "'.");
}
int main(int ac, char* av[])
{
try {
// Declare three groups of options.
options_description first("First flow options");
first.add_options()
("first", "first flow")
("opt1", "option 1")
("opt2", "option 2")
;
options_description second("Second flow options");
second.add_options()
("second", "second flow")
("opt3", "option 3")
("opt4", "option 4")
;
// Declare an options description instance which will include
// all the options
options_description all("Allowed options");
all.add(first).add(second);
variables_map vm;
store(parse_command_line(ac, av, all), vm);
conflicting_options(vm, "first", "second");
conflicting_options(vm, "first", "opt3");
conflicting_options(vm, "first", "opt4");
conflicting_options(vm, "second", "opt1");
conflicting_options(vm, "first", "opt2");
}
catch(std::exception& e)
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
But I have too many options, so, it will be nice to do checks only for groups.
Thanks in advance!