API Reference
Setup
-
argslib.ArgParser(helptext=None, version=None) -
Initializes a new
ArgParserinstance. Supplying help text activates an automatic--helpflag; supplying a version string activates an automatic--versionflag. (Automatic-hand-vshortcuts are also activated unless registered by other options.) -
.parse() -
Parses the application's command line arguments. Raises
ArgsErrorif any of the arguments are not valid unicode strings.
Flags and Options
-
.flag(name) -
Registers a new flag. The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts. -
.option(name, type=str, default=None) -
Registers a new option. The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts. Options are string-valued by default but thetypeparameter can be changed toint,float, or any other callable which can parse a string value. A default value can be specified which will be used if the option is not found.
Retrieving Values
-
.count(name) -
Returns the number of times the specified flag or option was found. Raises
ArgsErrorifnameis not a recognised flag or option name. -
.found(name) -
Returns
Trueif the specified flag or option was found,Falseif not. RaisesArgsErrorifnameis not a recognised flag or option name. -
.value(name) -
Returns the value of the specified option. Returns the default value if the option was not found. Raises
ArgsErrorifnameis not a recognised option name. -
.values(name) -
Returns the specified option's list of values. Raises
ArgsErrorifnameis not a recognised option name.
Positional Arguments
-
.args -
Stores the positional arguments as a list of strings.
Commands
-
.command(name, helptext=None, callback=None) -
Registers a new command. The
nameparameter accepts an unlimited number of space-separated aliases. Returns the command'sArgParserinstance which can be used to register the command's flags and options. If the command is found, thecallbackfunction will be called with the command's name andArgParserinstance as arguments. -
.command_name -
Stores the command name if a command was found, otherwise
None. -
.command_parser -
Stores the command parser if a command was found, otherwise
None. -
.enable_help_command -
This boolean switch toggles support for an automatic
helpcommand that prints subcommand helptext. The value defaults tofalsebut gets toggled automatically totruewhenever a command is registered. You can use this switch to disable the feature if required.