A simple-to-use command line argument parser written in modern C++.
You can create a parser using the builder pattern. There are three different kinds of arguments:
- flags (
true
orfalse
), - named parameters, and
- optionally named parameters.
Just concatenate the parameters you need while creating the parser and finish your call with create()
. Please take a look at this example:
auto parser = arguably::create_parser()
.optionally_named<'f', "first", "This is the first parameter", std::string>("-")
.named<'n', "named", "This is some named parameter", std::string>("-")
.optionally_named<'s', "second", "This is the second parameter", std::string>("-")
.flag<'o', "option", "This is some flag">()
.optionally_named<'t', "third", "This is the third parameter", std::string>("-")
.create();
parser.parse(argv);
if (parser) { // did the parsing succeed?
std::cout << parser.get<'f'>() << "\n"; // prints the argument passed to 'f' (or the provided default value)
}
For flags, you just do:
.flag<'f', "flag", "this is the description">()
For named parameters, you just do:
.named<'n', "named", "this is the description", std::string>("default-value")
For optionally named parameters, you just do:
.optionally_named<'n', "named", "this is the description", std::string>("default-value")
This library does compile-time-checks. For example, the following code won't compile:
auto parser = arguably::create_parser()
.flag<'a', "a_flag", "this is a description">()
.create();
parser.parse(argv);
std::cout << parser.get<'b'>() << "\n"; // <- won't compile, because 'b' is no valid command abbreviation