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 executable name. auto name = ookii::command_line_parser::get_executable_name(argc, argv); auto parser = ookii::parser_builder{name} .add_argument(required_argument, "Required").required().positional() .description("A required positional argument.") .add_argument(optional_argument, "Optional").positional() .description("An optional positional argument.") .add_argument(named_argument, "Named") .description("An argument that can only supplied by name.") .add_argument(switch_argument, "Switch") .description("A switch argument, which doesn't require a value.") .build();
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] // [name_transform: PascalCase] struct arguments { // [argument, required, positional] // A required positional argument. std::string required; // [argument, positional] // An optional positional argument. int optional; // [argument] // An argument that can only supplied by name. float named; // [argument: Switch] // A switch argument, which doesn't require a value. bool switch_argument; OOKII_GENERATED_METHODS(arguments); };