API Reference
Initialization
-
ArgParser() -> ArgParser -
Initializes a new
ArgParserinstance. All the setup methods below return the parser instance to enable chaining.
Setup
-
.helptext(_ text: String) -> ArgParser -
Registers a helptext string for the parser. Supplying a helptext string activates support for an automatic
--helpflag, also a-hshortcut if not registered by another option. -
.version(_ text: String) -> ArgParser -
Registers a version string for the parser. Supplying a version string activates support for an automatic
--versionflag, also a-vshortcut if not registered by another option. -
.flag(_ name: String) -> ArgParser -
Registers a new flag. The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts. -
.option(_ name: String) -> ArgParser -
Registers a new option. The
nameparameter accepts an unlimited number of space-separated aliases and single-character shortcuts. -
.command(_ name: String, _ cmdParser: ArgParser) -> ArgParser -
Registers a new command. The
nameparameter accepts an unlimited number of space-separated aliases. The command's helptext, flags, and options can be registered on the command'sArgParserinstance. -
.callback(_ cb: (String, ArgParser) -> Void) -> ArgParser -
Registers a callback function on a command parser. If the command is found the function will be called and passed the command's name and the command's
ArgParserinstance. -
.enableHelpCommand(_ enable: Bool) -> ArgParser -
This boolean switch toggles support for an automatic
helpcommand that prints subcommand helptext. The value defaults tofalsebut gets toggled automatically totruewhenever a command with helptext is registered. You can use this method to disable the feature if required.
Parsing
-
.parse(_ args: [String]? = nil) -
Parses an array of arguments. Defaults to parsing the application's command line arguments.
Option Values
-
.found(_ name: String) -> Bool -
Returns true if the specified flag or option was found.
-
.count(_ name: String) -> Int -
Returns the number of times the specified flag or option was found.
-
.value(_ name: String) -> String? -
Returns the value of the specified option or
nilif the option was not found. -
.values(_ name: String) -> [String] -
Returns the specified option's list of values. This will be empty if the option was not found.
Positional Arguments
-
.args: [String] -
Stores positional arguments. This will be empty if no positional arguments were found.
Commands
-
.commandName: String? -
Stores the command's name, if a command was found.
-
.commandParser: ArgParser? -
Stores the command's
ArgParserinstance, if a command was found.