Ookii.CommandLine for C++
1.0.0
|
Specifies options for an argument under construction. More...
#include <ookii/command_line_builder.h>
Public Member Functions | |
argument_builder (basic_parser_builder &basic_parser_builder, string_type name, value_type &value) | |
Initializes a new instance of the argument_builder class. More... | |
argument_builder & | alias (string_type alias) |
Adds an alias to the argument. More... | |
argument_builder & | cancel_parsing () |
Indicates that supplying this argument will cancel parsing. More... | |
argument_builder & | converter (converter_type converter) |
Supplies a custom function to convert strings to the argument's type. More... | |
argument_builder & | default_value (element_type default_value) |
Sets a default value for the argument, which will be used if the argument is not supplied.. More... | |
argument_builder & | description (string_type description) |
Sets the long description for the argument. More... | |
argument_builder & | positional () |
Indicates that the argument can be specified by position. More... | |
argument_builder & | required () |
Indicates that the argument is required. More... | |
argument_builder & | value_description (string_type value_description) |
Sets the value description for the argument. More... | |
Specifies options for an argument under construction.
This class inherits from argument_builder_base for arguments that are not multi-value, and inherits from multi_value_argument_builder for arguments that are.
|
inline |
Initializes a new instance of the argument_builder class.
basic_parser_builder | A reference to the basic_parser_builder used to build this argument. |
name | The name of the argument. |
value | A reference where the argument's value will be stored. |
|
inline |
Adds an alias to the argument.
alias | The alias to add. |
An alias is an alternate, often shorter, name for an argument that can be used to specify it on the command line. For example, an argument -Verbose
might have the alias -v
.
An argument can have multiple aliases, which can be specified by invoking this method multiple times.
|
inline |
Indicates that supplying this argument will cancel parsing.
If set, if this argument is supplied, argument parsing will immediately return with parse_error::parsing_cancelled. Arguments after this one will not be evaluated.
This can be used, for example, to implement a -Help
argument where usage help should be shown when supplied even if the rest of the command line is valid.
Note that parsing will be cancelled regardless of the supplied value. So, e.g. for a switch argument, -Help:false
will still cancel parsing and display help. If you want to conditionally cancel parsing, you can do so by using a callback with the basic_command_line_parser::on_parsed() method.
|
inline |
Supplies a custom function to convert strings to the argument's type.
converter | The function used for conversions. |
A conversion function must have the signature std::optional<T>(std::basic_string_view<CharType> value, const std::locale &loc)
. It should return std::nullopt if the conversion failed.
Even if a custom converter is used, conversion using the default method (stream extraction or a specialized lexical_convert template) must still be possible in order to avoid compiler errors. Use this method if a default conversion exists, but you wish to deviate from it for a specific argument.
|
inline |
Sets a default value for the argument, which will be used if the argument is not supplied..
default_value | The default value to use. |
The default value will be applied after parsing to all non-required arguments that weren't specified on the command line.
If no default value is set, basic_command_line_parser will not change the value of the variable holding the argument's value at all, so you can also initialize the variable to the desired value. The advantage of using default_value is that the value can be included in the usage help.
Setting a default value for a required argument is allowed, but the value will never be used.
|
inline |
Sets the long description for the argument.
description | The new description. |
The description is used when generating usage help, and is included in the list of descriptions after the usage syntax.
An argument without a description will not be included in the list of descriptions (but will still be included in the syntax).
|
inline |
Indicates that the argument can be specified by position.
Calling the positional() method will make the argument positional, assigning it the next available position. Therefore, you should define positional arguments in the order you want them to be specified.
A positional argument can still be specified by name, as well as by position.
If the argument is already positional, this method has no effect.
|
inline |
Indicates that the argument is required.
A required argument must be provided, otherwise parsing is not successful. Typically, required arguments should also be positional, but this is not mandatory.
If the argument is already required, this method has no effect.
|
inline |
Sets the value description for the argument.
value_description | The new value description. |
The value description is a brief, often one-word description of the type of values that the argument accepts. It's used as part of the usage help's syntax. An example of a value description could be "number", "string", "name", or someting similarly simple.
For longer descriptions of the argument's purpose, use the description() method instead.
If not specified explicitly, the value description defaults to the short type name (excluding namespace names) of the argument's type. The exact value may depend on your compiler.
The default value description for a type can also be overridden by specializing the value_description template.