-
Notifications
You must be signed in to change notification settings - Fork 591
Quick start
First, you only need to include:
#include <cxxopts.hpp>
Then, make an instance of cxxopts::Options
:
cxxopts::Options options;
You can then add options like so:
bool compile = false;
options.add_options()
("f,file", "File", cxxopts::value<std::string>())
("c,compile", "Compile", cxxopts::value(compile));
The options can be specified with a short option, which is a single character, or a long option, which is two or more characters, or both, separated by a comma; the short option must come first.
To parse the command line arguments, simple write:
auto result = options.parse(argc, argv);
This will parse the arguments and modify argc
and argv
, removing all recognised arguments.
To get the number of times that an option appeared on the command line, use:
result.count("option");
To retrieve the parsed value, it can be accessed with operator[]
. The value returned is a generic value which must be converted to the type of the value stored. For example, for the option "file" above, whose value is a string, it must be converted to a string using:
result["file"].as<std::string>();