Command Line Interface
Options
An option can have an unlimited number of long-form aliases and single-character shortcuts: --option
, -o
.
Option values can be separated by either a space, --opt 123
, or an equals symbol, --opt=123
. Either syntax can be used with shortcuts: -o 123
, -o=123
.
Multiple shortcuts can be condensed into a single block, e.g. -abc foo bar
. Trailing arguments are consumed in sequence as required by the options.
Options are registered with default values which are used if the option is not found. If an option is found multiple times its value is the final value encountered.
Multivalued Options
Options can be treated as singular or multivalued as circumstances require. Each option maintains an internal list to which newly parsed values are appended; the (singular) value of the option is the final value in the list or the default value if the list is empty.
For example, in the command below:
$ myapp --foo abc --foo def
the value of the option foo
is "def"
but the array ["abc", "def"]
is also available for use if required.
Flags
Flags are valueless options — they're either present or absent, but take no arguments. Like options, flags can have an unlimited number of long-form aliases and single-character shortcuts: --flag
, -f
.
Positional Arguments
Options and flags can be preceded by, followed by, or interspaced with positional arguments which are assembled by the parser into an array of strings.
The parser supports the standard --
switch for turning off option-parsing. All arguments following a --
will be treated as positional arguments, even if they begin with a single or double dash.
Commands
This library supports git-style command interfaces with arbitrarily-nested commands. Commands have builtin support for an automatic --help
flag and an automatic help <cmd>
command, i.e. the commands
$ myapp <cmd> --help
and
$ myapp help <cmd>
are functionally identical and will both print the help text registered with the command.
Negative Numbers
Some argument-parsing libraries struggle with negative numbers — for example, they will try to parse -3
as a flag or option named 3
. This library always treats arguments beginning with a dash and a digit as positional arguments or option values, never as flag or option names.