Options can have an unlimited number of long-form aliases and single-character shortcuts:
Options can have string, integer, or floating-point values.
Option values can be separated by either a space,
--option 123, or an equals symbol,
--option=123. Either syntax can be used with shortcuts:
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 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:
$ my_app --foo 123 --foo 456
the value of the option
456 but the list
[123, 456] is also available for use if required.
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:
Options and flags can be preceded by, followed by, or interspaced with positional arguments.
This library 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.
Note that you can use quotes to surround argument and option values containing spaces, e.g.
$ my_app "arg 1" "arg 2" $ my_app --option "value with spaces" $ my_app --option="value with spaces" $ my_app -o="value with spaces"
This isn't a feature of the library — the quotes are processed by the shell and the library only sees the resulting string values, including spaces.
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
$ my_app <cmd> --help
$ my_app help <cmd>
are functionally identical and will both print the help text registered with the command.
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.