Supported types
When command line entries are mapped to the annotated data members, the text value is converted to the type of the data member.
Boolean flags
Boolean types usually represent command line flags. argparse
supports multiple ways of providing flag value including negation (i.e., --no-flag
):
Command line entries | Result |
---|---|
|
|
|
|
|
|
| error |
argparse
supports the following strings as a <value>
(comparison is case-insensitive):
| Result |
---|---|
| true |
| false |
Numbers and strings
Numeric (according to std.traits.isNumeric
) and string (according to std.traits.isSomeString
) data types are seamlessly converted to destination type using std.conv.to
:
Arrays
argparse
supports 1D and 2D arrays:
If an argument is bound to 1D array, a new element is appended to this array each time the argument is provided in command line.
In case of 2D array, new elements are grouped in a way as they appear in command line and then each group is appended to this array.
The difference can be easily shown in the following example:
Alternatively one can set Config.valueSep
to allow multiple elements in one command line entry:
Associative arrays
argparse
also supports associative array where simple value type (e.g. numbers, strings etc.). In this case, expected format of the value is key=value
(equal sign can be customized with Config.assignChar
):
Alternatively one can set Config.valueSep
to allow multiple elements in one command line entry:
Enums
It is encouraged to use enum
types for arguments that have a limited set of valid values. In this case, argparse
validates that the value specified in command line matches one of enum identifiers:
In some cases the value for command line argument might have characters that are not allowed in enum identifiers. Actual values that are allowed in command line can be adjusted with ArgumentValue
UDA:
Counter
Counter is an argument that tracks the number of times it's specified in the command line:
The same example with enabled bundling:
Callback
If member type is a function, argparse
will try to call it when the corresponding argument is specified in the command line.
argparse
supports the following function signatures (return value is ignored, if any):
... func()
- argument is treated as a boolean flag.... func(string)
- argument has exactly one value. The value specified in command line is provided intostring
parameter.... func(string[])
- argument has zero or more values. Values specified in command line are provided intostring[]
parameter.... func(RawParam)
- argument has zero or more values. Values specified in command line are provided into parameter.
Example:
Custom types
argparse
can actually work with any arbitrary type - just provide parsing function (see Parsing customization for details):