API Reference
- Setup
- Registering Flags and Options
- Parsing
- Retrieving Flag and Option Values
- Positional Arguments
- Command Setup
- Command Inspection
Setup
-
ArgParser() -
Creates a new argument parser.
You can customize an ArgParser using the methods below:
-
:helptext() -> str?
:helptext(text: str) -> ArgParser -
Gets or sets the parser's helptext string.
- If called with no arguments, returns the parser's helptext string.
- If called with a single argument, sets the parser's helptext string. Returns the parser to enable chaining.
Specifying a
helptextstring for a parser activates an automatic--helpflag; also a-hshortcut unless registered by another option. -
:version() -> str?
:version(text: str) -> ArgParser -
Gets or sets the parser's version string.
- If called with no arguments, returns the parser's version string.
- If called with a single argument, sets the parser's version string. Returns the parser to enable chaining.
Specifying a
versionstring for a parser activates an automatic--versionflag; also a-vshortcut unless registered by another option. -
:enable_help_command(enable: bool) -> ArgParser -
Toggles the
enable_help_commandflag.This boolean flag 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.Returns the parser to enable chaining.
Registering Flags and Options
You can register flags and options on an ArgParser using the methods below:
-
:flag(name: str) -> ArgParser -
Registers a new flag.
- A flag is a boolean option that is either present or absent but takes no argument.
-
The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts.
Returns the parser to enable chaining.
-
:option(name: str) -> ArgParser
:option(name: str, default: any) -> ArgParser
:option(name: str, default: any, parser: callable(str) -> any) -> ArgParser -
Registers a new option.
-
The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts. -
If you don't specify a
defaultvalue for an option, its default value will benull. -
If you specify a
parserfor an option, it should be a callable that takes a single string argument (the option's value) and returns the parsed value. It can panic or return anerrto indicate failure. -
You can specify the builtin function
$i64to parse an option's value as an integer or the builtin function$f64to parse an option's value as a float.
Returns the parser to enable chaining.
-
The
Parsing
Call the :parse() method on the root ArgParser instance to parse the program's command line arguments:
-
:parse(args: iterable?[str] = null) -> err? -
Parses a sequence of string arguments. If
argsis omitted, this method defaults to parsing the program's command line arguments.Returns an
errif parsing fails, otherwisenull.You only need to call this method on the root
ArgParserinstance; arguments for commands will be parsed automatically.
Retrieving Flag and Option Values
You can use any of a flag or option's registered aliases or shortcuts as the name parameter in the calls below.
-
:count(name) -> i64 -
Returns the number of times the specified flag or option was found.
-
:found(name) -> bool -
Returns
trueif the specified flag or option was found,falseif not. -
:value(name) -> any -
Returns the value of the specified option.
- Returns the option's default value if the option was not found.
- If multiple values were found, returns the final value to be parsed.
-
:values(name) -> vec -
Returns the specified option's list of values. If the option was not found, the vector will be empty.
Positional Arguments
Positional arguments are appended to the parser's .args vector.
-
.args: vec[str] -
Stores positional arguments as a vector of strings.
Command Setup
Register a new command on a parent ArgParser instance using the :command() method:
-
:command(name: str) -> ArgParser -
Registers a new command on an
ArgParserinstance.-
The
nameparameter accepts an unlimited number of space-separated aliases for the command. -
Returns the command's
ArgParserinstance which can be used to register the command's help text, flags, options, and callback function.
-
The
Register a callback function for a command on the command's ArgParser instance using the :callback() method:
-
:callback(callback: callable(str, ArgParser) -> any) -> ArgParser -
Sets the callback function for a command parser.
If the command is found, this function will be called with the command's name and
ArgParserinstance as arguments.Returns the parser to enable chaining.
Command Inspection
If a command is found, the following fields are set on the command's parent ArgParser instance:
-
.command_name: str? -
If a command was found, this field stores the command name, otherwise
null. -
.command_parser: ArgParser? -
If a command was found, this field stores the command's
ArgParserinstance, otherwisenull.