Clio

Clio is an argument-parsing library designed for building elegant command-line interfaces.

C++



To use Clio in your C++ application add the clio.cpp and clio.h files to your source folder and include the header file:

#include "clio.h"

The header exports a clio::ArgParser class which provides the public interface to the library.

Clio is written in portable C++ 11.

Basic Usage

Initialize an argument parser, optionally specifying the application's help text and version:

clio::ArgParser(string helptext, string version)

Supplying help text activates an automatic --help flag; supplying a version string activates an automatic --version flag. An empty string can be passed for either parameter.

You can now register your application's options and commands on the parser using the registration functions described below. Once the required options and commands have been registered, call the parser's parse() method to process the application's command line arguments:

void ArgParser::parse(int argc, char **argv)

The arguments are assumed to be argc and argv as supplied to main(), i.e. the first element of the array is assumed to be the application name and will be ignored.

Parsed option values can be retrieved from the parser instance itself.

Register Options

Clio supports long-form options, --foo, with single-character shortcuts, -f.

An option can have an unlimited number of long and short-form aliases. Aliases are specified via the name parameter which accepts a string of space-separated alternatives, e.g. "foo f".

Option values can be separated on the command line by either a space, --foo 123, or an equals symbol, --foo=123.

void ArgParser::addFlag(string name)

Register a flag (a boolean option) with a default value of false. Flag options take no arguments but are either present (true) or absent (false).

void ArgParser::addFloat(string name, double value)

Register a floating-point option with a default value.

void ArgParser::addInt(string name, int value)

Register an integer option with a default value.

void ArgParser::addStr(string name, string value)

Register a string option with a default value.

Register List Options

List options store multiple values. Greedy list options attempt to parse multiple consecutive arguments.

Like single-valued options, list options can have an unlimited number of long and short-form aliases specified via the name parameter.

void ArgParser::addFlagList(string name)

Register a boolean list option.

void ArgParser::addFloatList(string name, bool greedy = false)

Register a floating-point list option.

void ArgParser::addIntList(string name, bool greedy = false)

Register an integer list option.

void ArgParser::addStrList(string name, bool greedy = false)

Register a string list option.

Retrieve Option Values

An option's value can be retrieved from the parser instance using any of its registered aliases.

bool ArgParser::found(string name)

Returns true if the specified option was found while parsing.

bool ArgParser::getFlag(string name)

Returns the value of the specified boolean option.

double ArgParser::getFloat(string name)

Returns the value of the specified floating-point option.

int ArgParser::getInt(string name)

Returns the value of the specified integer option.

string ArgParser::getStr(string name)

Returns the value of the specified string option.

Retrieve List Values

A list-option's values can be retrieved from the parser instance using any of its registered aliases.

vector<bool> ArgParser::getFlagList(string name)

Returns the specified option's list of values.

vector<double> ArgParser::getFloatList(string name)

Returns the specified option's list of values.

vector<int> ArgParser::getIntList(string name)

Returns the specified option's list of values.

vector<string> ArgParser::getStrList(string name)

Returns the specified option's list of values.

int ArgParser::lenList(string name)

Returns the length of the specified option's list of values.

Set Option Values

The methods below can be used to set option values manually.

Note that, internally, all options are list-options. An option's 'value' is simply the last value in its internal list.

void ArgParser::clearList(string name)

Clear the specified option's internal list of values.

void ArgParser::setFlag(string name, bool value)

Append a boolean value to the specified option's internal list.

void ArgParser::setFloat(string name, double value)

Append a floating-point value to the specified option's internal list.

void ArgParser::setInt(string name, int value)

Append an integer value to the specified option's internal list.

void ArgParser::setStr(string name, string value)

Append a string value to the specified option's internal list.

Retrieve Positional Arguments

The methods below provide access to positional arguments identified by the parser.

string ArgParser::getArg(int index)

Returns the positional argument at the specified index.

vector<string> ArgParser::getArgs()

Returns the positional arguments as a list of strings.

vector<double> ArgParser::getArgsAsFloats()

Attempts to parse and return the positional arguments as a list of floats. Exits with an error message on failure.

vector<int> ArgParser::getArgsAsInts()

Attempts to parse and return the positional arguments as a list of integers. Exits with an error message on failure.

bool ArgParser::hasArgs()

Returns true if at least one positional argument has been found.

int ArgParser::lenArgs()

Returns the length of the positional argument list.

Set Positional Arguments

The methods below provide manual write access to the list of positional arguments.

void ArgParser::clearArgs()

Clears the list of positional arguments.

void ArgParser::appendArg(string arg)

Appends a string to the list of positional arguments.

Commands

Clio supports git-style command interfaces with arbitrarily-nested commands. Register a command on a parser instance using the addCmd() method:

ArgParser& ArgParser::addCmd(string name, string helptext, clio::callback cb)

This method returns a reference to a new ArgParser instance associated with the command. You can register the command's flags and options on this sub-parser using the standard methods listed above. (Note that you never need to call parse() on a command parser — if a command is found, its arguments are parsed automatically.)

The following ArgParser methods are also available for processing commands manually:

string ArgParser::getCmdName()

Returns the command name, if the parser has found a command.

ArgParser& ArgParser::getCmdParser()

Returns the command's parser instance, if the parser has found a command.

ArgParser& ArgParser::getParent()

Returns a command parser's parent parser.

bool ArgParser::hasCmd()

Returns true if the parser has found a command.