Command-line argument parser written in C++20.
Uses C++20 concepts and constraints to handle command-line argument parsing with static type-checking.
using VerboseFlag = CLArgs::Flag<"--verbose,-v", "Enable verbose output">;
using ConfigOption = CLArgs::Option<"--config,--configuration,-c", "<filepath>", "Specify config file", std::filesystem::path>;
int
main(int argc, char **argv)
{
CLArgs::Parser<VerboseFlag, ConfigOption> parser;
try
{
parser.parse(argc, argv);
}
catch (std::exception &e)
{
std::cerr << "Error: " << e.what() << '\n';
std::cerr << parser.help() << std::endl;
return EXIT_FAILURE;
}
const bool has_verbose = parser.has_flag<VerboseFlag>();
std::cout << "Has verbose option: " << std::boolalpha << has_verbose << "\n";
if (const auto config = parser.get_option<ConfigOption>(); config.has_value())
{
std::cout << "Config file: " << config.value() << std::endl;
}
}
For full examples, see the examples/ directory
Static type-checking ensures proper usage and prevents building with invalid or unsupported ValueTypes.
All arguments are parsed, validated, cast, and stored during the parse method. This is the only potential point of failure, meaning that if the application makes it past that function call with no exceptions, everything should be fine.