ArgParser

A ridiculously simple argument-parsing library for Swift.

Version 3.0.0

API Reference



Initialization

ArgParser() -> ArgParser

Initializes a new ArgParser instance. 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 --help flag, also a -h shortcut 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 --version flag, also a -v shortcut if not registered by another option.

.flag(_ name: String) -> ArgParser

Registers a new flag. The name parameter accepts an unlimited number of space-separated aliases and single-character shortcuts.

.option(_ name: String) -> ArgParser

Registers a new option. The name parameter accepts an unlimited number of space-separated aliases and single-character shortcuts.

.command(_ name: String, _ cmdParser: ArgParser) -> ArgParser

Registers a new command. The name parameter accepts an unlimited number of space-separated aliases. The command's helptext, flags, and options can be registered on the command's ArgParser instance.

.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 ArgParser instance.

.enableHelpCommand(_ enable: Bool) -> ArgParser

This boolean switch toggles support for an automatic help command that prints subcommand helptext. The value defaults to false but gets toggled automatically to true whenever 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 nil if 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 ArgParser instance, if a command was found.