|
kaycxx-cli
C++ CLI library
|
A flag does not take a value. An option takes a value and converts it to its declared C++ type.
Register a flag with a long name and an optional one-character alias.
The description is optional. A flag without a description remains parseable and is listed in generated help without explanatory text.
Both --quiet and -q set the first flag. Read flags with args::get. An absent flag returns false.
Flags do not accept values. For example, --quiet=true is rejected.
The option template argument defines the parsed value type. The value name is used as a placeholder in help and error output.
The parser accepts long option values as the next argument or after =. Short option values are read from the next argument.
Grouped short flags such as -qv and attached short option values such as -j4 are rejected. Write them as separate arguments instead.
Set a default through the returned handle. Handle configuration methods return the handle again, so the configured handle can be stored directly.
An option with a default is always available through args::get. For an option without a default, check args::has before reading it.
Generated help appends the rendered default value to the option description:
An ordinary option may occur only once. Repeating it is reported as a parsing error.
Use required() when an option must be specified explicitly. Required options are included directly in generated usage text and marked as required in the detailed help output.
For example, the generated help contains:
Required option validation is delayed until args::validate() is called. This allows the application to handle actions such as help and version before missing required input is reported.
When a required option is missing, validation reports Missing option --email <EMAIL>. A required option cannot also have a default value; combining required() and default_value() in either order throws std::invalid_argument while configuring the command.
Use repeatable_option when the same option may occur multiple times. Its handle reads all converted values as a vector in command-line order.
For example, --include first -I second --include=third produces std::vector<std::string>{ "first", "second", "third" }. An absent repeatable option is unavailable through args::get unless it has a default value.
A repeatable option can also be required. It must then occur at least once and may still occur additional times.
Mark flags and options that select alternative command actions with action(). At most one explicitly specified action may occur on a command line. Omitting all actions is valid and lets the application perform its default action.
Normal switches are not part of this restriction and may be combined with an action. Default option values do not select an action. Only an action explicitly present on the command line participates in the exclusivity check.
Flags and options remain available after selecting descendant commands. This allows application-wide and command-wide switches to appear anywhere after the command on which they are defined.
In both forms, --color belongs to example and --verbose belongs to login. A child switch cannot appear before its command because that command has not been selected yet.
Help for a command lists its own switches followed by switches from its ancestors. Required and optional parent options follow the same scope rules. Defaults and required-option validation apply only along the selected command path.
A command cannot define a long switch name or short alias already used by an ancestor or descendant because its meaning would otherwise depend on its position. Sibling commands may reuse switch names because only one sibling can be selected.