Using boost program_options, select sets of options based upon one mandatory 'mode' option

635 views Asked by At


Lets presume I have a number of option groups called modeA modeB common. I'll refer to these within {} - ie {common} expands to any option described in that group.

I'd like to be able to enforce the following command lines

command A {common} {modeA}
command B {common} {modeB}

The position of options from the mode and common groups is not important and can be intertwined
The following would fail

command A {modeB}    /* Wrong option group for this mode */
command A B          /* A and B not allowed */

Thus the objectives are

  • an option that must occur and be one of the set A|B
    • if possible forced to be the first parameter
  • whole groups parsed as Ok or ignored based upon the above mandatory parameter
2

There are 2 answers

0
Gray-M On BEST ANSWER

Not an elegant solution but ...
Divide the options into at least three groups, options for modeA/modeB and others. Others contain help and the mode optons. Use a custom validator for mode to limit the options (with a default) Then

  • Combine all groups and parse
  • Action --help if required (Explain all modes/options)
  • Determine mode
  • Make new group combination leaving out unrequired mode and re-parse

I feel there must be a slicker way

0
Urkle On

I am working to implement this as well, and the best I can figure is to

  1. parse the "core" options first but allowing unrecognized options
  2. determine the mode
  3. collect the unrecognized options via collect_unrecognized
  4. parse the unrecognized options against the mode options disallowing unrecognized (thus triggering an error if mode B options are used in mode A)