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
25
u/JVApen Clever is an insult, not a compliment. - T. Winters Apr 25 '26
I thought other implementations of this existed, including in the reflection paper. It would be interesting to compare those
21
u/G6L20 Apr 25 '26
Here is my version https://github.com/Garcia6l20/reflex/tree/main/cli With autocompletion support π
6
u/Specific-Housing905 Apr 25 '26
What compiler can I use with it?
Might be worth to add it to the readme on github.
2
u/TrnS_TrA TnT engine dev Apr 25 '26
Nice. Love your videos sir!
3
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Apr 26 '26
Cool stuff! Just for funsies, here's a C++20 version using Boost.PFR: https://gcc.godbolt.org/z/3P6z83Kzc
Usage looks like this:
The field metadata is non-zero sized at the moment, but could turned into some sort of sentinel type using NTTPs + some constexpr string.
3
u/pdimov2 Apr 26 '26
It shouldn't be necessary to use template parameters for the values:
[[= clap::Description<"host to connect to">{}]][[= clap::Description("host to connect to")]]or[[= clap::Description{"host to connect to"}]]should work.2
u/Spartan322 Apr 26 '26
Pretty sure you could make things like
clap::ShortNameandclap::Envintoclap::ShortName_tandclap::Env_tand then doThen you could do
[[=clap::ShortName<'p'>]]and[[=clap::Env<"RETRY_COUNT">]]instead.1
u/gosh Apr 25 '26
If you want inspiration about functionality to add so here is one
Documentation: gd::cli::options β Command-Line Interface Parser
code
I needed to create my own logic parsing arguments because there wasn't any library done with basic logic I think a simple wrapper should have.
1
u/User_Deprecated Apr 26 '26
I've got a protocol parser where every message type is a hand-written struct, and the tag-to-field mapping basically just lives in comments next to each member. Adding a field means touching the struct, the tag metadata, the header switch, and the builder. It's not the end of the world but it's the kind of thing you forget one of and then spend a while figuring out why parsing silently drops a field.
With annotations like this the struct could probably just be the source of truth for all of that. Have you seen compile time issues on gcc trunk with larger structs? Feels like once you go past 40 or 50 members, template instantiation might get pretty heavy.
-30
u/OutlandishnessNo8034 Apr 25 '26
2
u/[deleted] Apr 26 '26
[removed] β view removed comment