ArgParser

A ridiculously simple argument-parsing library for Swift.

Version 2.0.0

API Reference



Setup

ArgParser()

Initializes a new ArgParser instance. All the setup methods below return the parser instance to enable chaining.

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

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.

Positional Arguments

.args: [String]

Stores positional arguments.

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.

.helpCommand: Bool

Boolean switch; activates support for an automatic help command which prints subcommand helptext. (Defaults to false but gets toggled to true when a command with helptext is registered.)