argparse icon indicating copy to clipboard operation
argparse copied to clipboard

`Argument` should expose the name of the flag

Open Jacobfaib opened this issue 1 year ago • 0 comments

It would be useful for Argument to expose the flag name(s) back to the user. A common use-case for this is DRY-ness when referencing the flags after parsing, or during error handling:

auto foo_flag = std::string_view{"--foo"};
auto arg_foo& = parser.add_argument(foo_flag, ...);
// ...
if (parser[foo_flag] != "bad") {
  throw std::runtime_error{"invalid value for " + foo_flag + ": ..."};
}

Currently users are forced to wrap the argument class to avoid repeating information:

class Argument {
  std::string_view flag;
  argparse::Argument *arg;
};

Argument arg;

arg.flag = "--foo";
arg.arg = &parser.add_argument(arg.flag, ...);

Jacobfaib avatar Jan 06 '25 16:01 Jacobfaib