Command Line Interface
- Options
- Multivalued Options
- Flags
- Positional Arguments
- Quoted Values
- Commands
- Non-Unicode Arguments
- Negative Numbers
Options
Options can have an unlimited number of long-form aliases and single-character shortcuts: --option
, -o
.
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: -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.
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:
$ my_app --foo 123 --foo 456
the value of the option foo
is 456
but the list [123, 456]
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.
Argslib 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.
Quoted Values
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.
Commands
Argslib 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
and
$ my_app help <cmd>
are functionally identical and will both print the help text registered with the command.
Non-Unicode Arguments
To keep its API simple, this library only works with command line arguments which are valid unicode strings. If the parser finds an argument which is not a valid unicode string the .parse()
method will raise an ArgsError
exception.
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.