Config
argparse
provides decent amount of settings to customize the parser. All customizations can be done by creating Config
object with required settings (see below) and passing it to CLI API.
Assign character
Config.assignChar
is an assignment character used in arguments with value: -a=5
, -boo=foo
.
Default is equal sign =
.
Example:
Value separator
Config.valueSep
is a separator that is used to extract argument values: -a=5,6,7
, --boo=foo,far,zoo
.
Default is ,
.
Example:
Named argument prefix
Config.namedArgPrefix
is a character that named arguments begin with.
Default is dash (-
).
Example:
End of named arguments
Config.endOfNamedArgs
is a string that marks the end of all named arguments. All arguments that are specified after this one are treated as positional regardless to the value which can start with namedArgPrefix
(dash -
by default) or be a subcommand.
Default is double dash (--
).
Example:
Case sensitivity
Config.caseSensitive
controls whether the argument names are case-sensitive. By default they are and it can be changed by setting this member to false
.
Default is true
.
Example:
Bundling of single-character arguments
Config.bundling
controls whether single-character arguments (usually boolean flags) can be bundled together. If it is set to true
then -abc
is the same as -a -b -c
.
Default is false
.
Example:
Adding help generation
Config.addHelpArgument
can be used to add (if true
) or not (if false
) -h
/--help
argument. In case if the command line has -h
or --help
, then the corresponding help text is printed and the parsing is stopped. If CLI!(...).parseArgs(alias newMain)
or CLI!(...).main(alias newMain)
is used, then provided newMain
function will not be called.
Default is true
.
Example:
Help text from the first part of the example code above:
Styling mode
Config.stylingMode
controls whether styling for help text and errors should be enabled. It has the following type: enum StylingMode { autodetect, on, off }
:
Config.StylingMode.on
: styling is always enabled.Config.StylingMode.off
: styling is always disabled.Config.StylingMode.autodetect
: styling will be enabled when possible.
See ANSI coloring and styling for details.
Default value is Config.StylingMode.autodetect
.
Example:
Help text from the first part of the example code above:
Styling scheme
Config.styling
contains style for the text output (error messages and help text). It has the following members:
programName
: style for the program name. Default isbold
.subcommandName
: style for the subcommand name. Default isbold
.argumentGroupTitle
: style for the title of argument group. Default isbold.underline
.argumentName
: style for the argument name. Default islightYellow
.namedArgumentValue
: style for the value of named argument. Default isitalic
.positionalArgumentValue
: style for the value of positional argument. Default islightYellow
.errorMessagePrefix
: style for Error: prefix in error messages. Default isred
.
See ANSI coloring and styling for details.
Example:
Help text from the first part of the example code above:
Error handling
Config.errorHandler
is a handler function for all errors occurred during command line parsing. It is a function that receives string
parameter which would contain an error message.
The default behavior is to print error message to stderr
.
Example:
This code prints Detected an error: Unrecognized arguments: ["-b"]
to stderr
.