Ookii.CommandLine for C++

Ookii.CommandLine for C++ is a header-only library that enables comprehensive command line argument parsing for C++ applications. It allows you to easily define required, optional, positional and named arguments, parse the command line, and generate usage help which can be shown to the user.

Ookii.CommandLine for C++ is a port of Ookii.CommandLine for .Net, providing the same argument parsing semantics, but using an API that is suitable for C++.

To define a set of command line arguments, first you create variables that will hold their values. Then, you use the ookii::parser_builder class to construct an ookii::command_line_parser to parse those arguments, specifying things such as the argument names, whether or not an argument is required, and descriptions used to customize the usage help, among other options.

For example, the following code defines four arguments: a required positional argument, an optional positional argument, a named argument, and a switch argument:

std::string required_argument;
int optional_argument;
float named_argument;
bool switch_argument;

// argv[0] is used for the application name.
auto parser = ookii::parser_builder{argv[0]}
    .add_argument(required_argument, "Required").required().positional()
    .add_argument(optional_argument, "Optional").positional()
    .add_argument(named_argument, "Named")
    .add_argument(switch_argument, "Switch")

Ookii.CommandLine also provides code-generation scripts that can generate the above code using a specially annotated struct or class. For example, the below generates the same arguments as above:

// [arguments]
struct arguments
    // [argument, required, positional]
    std::string required;
    // [argument, positional]
    int optional;
    // [argument]
    float named;
    // [argument: Switch]
    bool switch_argument;