![]() |
CLI11 2.7.1
C++11 Command Line Interface Parser
|
The ->group("name") modifier on an option only changes where the option is printed in the help. An option group is a stronger tool: it is a real container that collects options and can carry requirements of its own.
add_option_group returns a pointer to the group. The description is optional. An option group is a specialization of App, so everything that works on an app or a subcommand works on a group as well: require_option, excludes, needs, disabled, callbacks, and nesting.
Options can be created directly on the group, exactly as on an app:
An option that already exists on the parent app can be moved into the group:
The option pointers must belong to the parent application of the group, or an error is generated. A subcommand can be moved in the same way, which removes it from its parent:
An app counts a whole option group as a single option for the purpose of require_option, and the group counts as used if any option or subcommand inside it was used. That is what makes groups useful: you can require one of three options in one group and one of three in another.
require_option(N) requires exactly N if N is positive, up to N if N is negative, and resets to "0 or more" for N=0. require_option(min, max) sets both ends, with a max of 0 meaning unlimited. require_option() with no argument requires 1 or more.
Disabling a group with ->disabled() turns off every option inside it. Groups can also contain other groups.
See the option_groups.cpp and ranges.cpp examples.
CLI::TriggerOn and CLI::TriggerOff connect groups, so that use of one group enables or disables another:
The second argument can also be a std::vector<App *>. These helpers are built from preparse_callback, enabled_by_default(), and disabled_by_default(); see Subcommands. Use them one time per group, since they replace any earlier use of those three functions. For anything more complex, write the preparse_callback yourself.
The group name controls how the help print treats the group:
Group names may not contain newlines or null characters.
A subcommand with an empty name behaves much like an option group:
If an option is not found in the main app, all nameless subcommands are searched as well. Because add_subcommand also accepts a std::shared_ptr<App>, a set of options can be defined in one component and merged into one or more apps. Multiple nameless subcommands are allowed, and their callbacks run only if some option from the group was parsed.
Two helpers manage the life cycle of an option that is on its way out:
A deprecated option keeps working. The help print marks it, and the first use prints a warning that names the replacement, if you gave one.
A retired option does nothing. If the option exists it is replaced with a dummy option that takes the same arguments, so old command lines still parse, and the first use prints a warning. See the retired.cpp example.