Mutually exclusive option groups

I am getting acquainted with the utility for advanced boost settings, and I wonder if there is a way to define mutually exclusive groups of options. Ie, my program has different threads:

program --first --opt1 --opt2 ...
program --second --opt3 --opt4 ...

So, my options for different threads are mutually exclusive. Is there a way to define mutually exclusive groups of options?

Of course, the code below will do this:

/* 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 would be nice to do checks only for groups.

Thanks in advance!

+5
source share

Source: https://habr.com/ru/post/1215036/


All Articles