Args++

A ridiculously simple argument-parsing library for C++.

Version 2.1.0

API Reference



Setup

This library is written in portable C++11. The header file exports an args::ArgParser class which provides the public interface to the library.

ArgParser(string helptext = "", string version = "")

Initialize an ArgParser instance. Supplying help text activates an automatic --help flag; supplying a version string activates an automatic --version flag. (Automatic -h and -v shortcuts are also activated unless registered by other options.)

void .parse(int argc, char **argv)

Parse the application's command line arguments. Arguments are assumed to be argc and argv as supplied to main(). Parsed option values can be retrieved from the parser instance itself.

Flags and Options

void .flag(string name)

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

void .option(string name, string fallback = "")

Registers a new option. The name parameter accepts an unlimited number of space-separated aliases and single-character shortcuts. A fallback value can be specified which will be used if the option is not found.

Retrieving Values

bool .found(string name)

Returns true if the specified flag or option was found.

int .count(string name)

Returns the number of times the specified flag or option was found.

string .value(string name)

Returns the value of the specified option. Returns the fallback value if the option was not found.

vector<string> .values(string name)

Returns the specified option's list of values.

Positional Arguments

vector<string> .args

Stores the positional arguments.

Commands

ArgParser& .command(string name, string helptext = "", callback = nullptr)

Registers a new command. The name parameter accepts an unlimited number of space-separated aliases. Returns a reference to the command's ArgParser instance — you can register the command's flags and options on this parser using the standard methods listed above. If the command is found the callback function will be called with the command's name and ArgParser instance.

bool .commandFound()

Returns true if a command was found.

string .commandName()

Returns the command name if a command was found.

ArgParser& .commandParser()

Returns the command's parser instance if a command was found.