Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument should expose the name of the flag #392

Open
Jacobfaib opened this issue Jan 6, 2025 · 0 comments
Open

Argument should expose the name of the flag #392

Jacobfaib opened this issue Jan 6, 2025 · 0 comments

Comments

@Jacobfaib
Copy link

Jacobfaib commented Jan 6, 2025

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, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant