crow

Utility library


Project maintained by CaptainCrowbar Hosted on GitHub Pages — Theme by mattgraham

Command Line Options

Crow Library by Ross Smith

#include "crow/options.hpp"
namespace Crow;

Contents

Options Class

class Options;

The Options class in this module handles parsing of command line options, and automatic generation of help messages for the user. It provides some commonly expected command line features:

A program will normally construct an Options object, and use multiple calls to add() to construct the option specification, before calling parse() to parse the actual command line arguments.

Example

int main(int argc, char** argv) {
    std::string alpha, omega;
    int number = 42;
    Options opt("My Program", "1.0", "Goes ding when there's stuff.");
    opt.add(alpha, "alpha", 'a', "The most important option");
    opt.add(omega, "omega", 0, "The least important option");
    opt.add(number, "number", 'n', "How many roads to walk down");
    if (! opt.parse(argc, argv))
        return 0;
    // ... main code ...
}

If this program is invoked with the --help option (or -h), it will display the following information on standard output:

My Program 1.0

Goes ding when there's stuff.

Options:

    --alpha, -a <arg>   = The most important option
    --omega <arg>       = The least important option
    --number, -n <int>  = How many roads to walk down (default 42)
    --help, -h          = Show usage information
    --version, -v       = Show version information

Member types

enum class Options::flag_type {
    none = 0,
    anon,
    no_default,
    required,
    dir_exists,
    file_exists,
    not_exists,
    parent_exists,
};
using enum Options::flag_type;

These are bitmasks that can be used in the flags argument of Options::add().

class Options::setup_error: public std::logic_error;
class Options::user_error: public std::runtime_error;

Exceptions thrown by Options member functions if anything goes wrong. The setup_error exception indicates that the programmer has made an error in configuring the options; user_error indicates that an invalid set of arguments were supplied on the command line.

Life cycle functions

Options::Options(const std::string& app, const std::string& version,
    const std::string& description, const std::string& extra = {});

Constructor. The arguments are the application name, a version number (which can be an arbitrary string, and may be empty), a description of the app (to be printed at the top of the help text), and optionally some further information (to be printed at the bottom of the help text).

This will throw setup_error if app or description is empty (or contains only whitespace characters).

Options::Options();
Options::Options(const Options& opts);
Options::Options(Options&& opts) noexcept;
Options::~Options() noexcept;
Options& Options::operator=(const Options& opts);
Options& Options::operator=(Options&& opts) noexcept;

Other life cycle functions.

Configuration functions

template <typename T> Options& Options::add(T& var,
    const std::string& name, char abbrev, const std::string& description,
    flag_type flags = flag_type::none, const std::string& group = {},
    const std::string& pattern = {});

Adds an option to the configuration. The arguments are:

The variable into which the parsed value is written must be one of the following types:

The initial value of the variable is used as a default if the option is not present on the command line. Behaviour is undefined if the variable’s value is changed after add() has been called on it (other than by parse()).

The Options class will automatically add the --help and --version options, after all caller-supplied options, with the abbreviations -h and -v if those have not been used by any previous options.

The add() function will throw setup_error under any of the following conditions:

If more than one of these error conditions exists for the same option, it is unspecified which one will be reported.

Default values are not checked against regex patterns or fie system requirements.

Behaviour is undefined if add() is called after parse().

void Options::auto_help() noexcept;

If this is set, an empty argument list will be interpreted as a request for help (equivalent to --help).

void Options::set_colour(bool b) noexcept;

This can be used to control whether or not the help output is colourised. By default, if help is requested, the parse() function will call isatty(1) to determine whether standard output has been redirected, and will emit colour codes only if it believes it is writing to a terminal. This function overrides the automatic detection.

Command line parsing functions

bool Options::parse(int argc, char** argv,
    std::ostream& out = std::cout);
bool Options::parse(std::vector<std::string> args,
    std::ostream& out = std::cout);

After the options have been configured, call one of these functions to parse the actual command line arguments. The first version takes the standard arguments from main() directly; the second takes a vector of argument strings (not including the command name). The output stream argument indicates where to write any help or version information requested by the user, defaulting to standard output.

The return value is true if the command line arguments have been successfully parsed and the program can continue processing. If parse() returns false, then help or version information has been written to the output stream, and the caller should stop processing and exit here (probably by returning from main()).

The parse() functions will throw user_error under any of the following conditions:

(If more than one of these error conditions exists for the same argument, it is unspecified which one will be reported.)

bool Options::found(const std::string& name) const;

True if the named option was found on the command line (the leading "--" is optional). This will always return false if the name does not match any of the configured options.