r/cpp • u/nathan_baggs • Apr 25 '26
Using Reflection For Parsing Command Line Arguments
I've been very excited about reflection so I built a small library to pass command line arguments
Basic example:
struct Args
{
std::string first_name;
int age;
bool active;
};
// ./program --first-name John --age 99 --active
const auto args = clap::parse<Args>(argc, argv);
assert(args.first_name == "John");
assert(args.age == 99);
assert(args.active);More interesting example:
struct Args
{
[[= clap::Description<"host to connect to">{}]]
std::string host = "localhost";
[[=clap::ShortName<'p'>{}]]
std::uint16_t port;
[[=clap::Env<"RETRY_COUNT">{}]]
std::uint32_t retry_count;
std::optional<std::string> log_file;
[[=clap::ShortName<'e'>{}]]
bool encrypted;
[[=clap::ShortName<'c'>{}]]
bool compressed;
[[=clap::ShortName<'h'>{}]]
bool hashed;
};
// ./program -p 8080 -ec
const auto args = clap::parse<Args>(argc, argv);
assert(args.host == "localhost");
assert(args.port == 8080);
assert(args.retry_count == std::stoul(std::getenv("RETRY_COUNT")));
assert(!args.log_file);
assert(args.encrypted);
assert(args.compressed);
assert(!args.hashed);The amount of code to handle this is actually quite minimal < 500 lines.
There's a few modern goodies that make this code work:
I guess we don't know what "idiomatic" reflection usage is like yet, I'm interested to come back to this code in a years time and see what mistakes I made!
Link to the code: https://github.com/nathan-baggs/clap
Any feedback, queries, questions are welcome!
128
Upvotes
View all comments
Show parent comment
3
u/h2g2_researcher Apr 27 '26
The reflection allows a lot of the grunt work in creating these kinds of parsers to be done at by the compiler instead of the programmer.
Using typical methods I'd have to hand-write a parser to convert
int argc, char** argv. Even using a library I would still have to write code like:and so on. This is liable to error-by-typo. If I write
get<std::string>("fist-name")instead, for example the compiler will be zero help to me. I could also make a copy-and-paste mistake end up withargs.active = ArgParser{argc,argv}.get<int>("age");which is wrong, but compiles fine!Even without that liability to make mistakes, this is still a bit of a pain in the backside to do for big argument lists. (Some programs have dozens and dozens valid arguments!)
And then every time I add a new argument to the
Argsstructure I also have to find the parsing function and remember to add parsing code to it. It can become a lot of work.The reflection element allows the compiler to make the connection. By setting up reflection sensibly the compiler can see that it is trying to fill a
std::stringfield namedfirst_name(usually this information is lost during the compilation process) and generate the parsing code to find thefirst-nameargument for you. And you can add new arguments understood by the program simply by adding them to theArgsstruct and the reflection handles updating the parsing function for you.The way it could bite you in the backside is if you update
int agetoint age_in_yearsthe expected command line argument will change and it will break existing use (unless you remember to do something to add aliases soageandage-in-yearsare treated the same on the command line).