C++ library for command line argument parsing.
- Project written in
c++20
, thereforeGCC 10.2
orClang 12.0.0
andlibstdc++10
orlibc++7
recommended.
- Run makefile
make
- Install library
make install
- Clone googletest into project root directory.
git clone https://github.com/google/googletest.git
- Run tests
make test
To use create an instance of Parser and add arg your app requires.
Parser parser = ParserBuilder("app_name").get();
parser.add_argument(ArgBuilder("cl_arg").get());
Parse command line arguments.
// ./app --cl_arg value
parser.parse(argc, argv);
Then you can access args value by its name.
auto res = parser.get("cl_arg");
Result is std::optional
. Contains value only if supplied or default value was given at the creation.
std::cout << res.value_or("was not given any value");
Besides named args (i.e. options), flags and positional args are available.
parser.add_flag(FlagBuilder("flag").get());
parser.add_positional_argument(PositionalArgBuilder("pos_arg").get());
// ./app pos_value -f
parser.parse(argc, argv);
assert(true == parser.was_toggled("flag"));
assert(std::string{"pos_value"} == parser.get("pos_arg").value());
NOTE: While creating positional argument it can be assigned default (current highest + 1 starting at 0) or user defined order number using
order()
method. Order only refers to position according to other positional arguments.
NOTE: Additionally positional args may be marked as required with
required()
function. While it makes no sense for required args to follow not required (unless implicitly forcing them to be required as well) it is not validated in any way.
Args can be supplied with conditions. Value will only be set if it meets all conditions.
auto is_whole_upper = [](const std::string& s) {
return std::all_of(s.begin(), s.end(), [](unsigned char c){
return !std::islower(c);
});
};
parser.add_argument(
ArgBuilder("upper")
.with_condition(Condition("IsWholeUpper", is_whole_upper))
.get()
);
try {
// ./app -u not_upper
parser.parse(argc, argv);
assert(false);
} catch(...) {}
More complex examples can be found in examples
directory.