Using boost program_options, select sets of options based upon one mandatory 'mode' option
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 failcommand 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 forc开发者_StackOverflow社区ed to be the first parameter
- whole groups parsed as Ok or ignored based upon the above mandatory parameter
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
I am working to implement this as well, and the best I can figure is to
- parse the "core" options first but allowing unrecognized options
- determine the mode
- collect the unrecognized options via collect_unrecognized
- parse the unrecognized options against the mode options disallowing unrecognized (thus triggering an error if mode B options are used in mode A)
精彩评论