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

ArgMatches::try_get_flag #5905

Open
2 tasks done
Lucretiel opened this issue Feb 7, 2025 · 3 comments
Open
2 tasks done

ArgMatches::try_get_flag #5905

Lucretiel opened this issue Feb 7, 2025 · 3 comments
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@Lucretiel
Copy link

Please complete the following tasks

Clap Version

4.5

Describe your use case

I'm dynamically populating a Command, based on the contents of a struct. Because it's happening dynamically, I'm using the try versions of the getters on ArgMatches, to handle the case where things happened incorrectly. However, there's no try version of get_flag, which I use for my bool fields.

Describe the solution you'd like

Would like to have a fallible try_get_flag on ArgMatches, to compliment all of the other try_ versions of things. Currently there's no panic-free way to get an argument that you're expecting to be a SetTrue.

Alternatives, if applicable

No response

Additional Context

No response

@Lucretiel Lucretiel added the C-enhancement Category: Raise on the bar on expectations label Feb 7, 2025
@epage
Copy link
Member

epage commented Feb 7, 2025

Is there a reason you aren't using try_get_one? get_flag is just a convenience helper for get_one. I lean towards people using try_get_one when they need more rather than adding many variants of helpers (also not a fan of all of the get_matches / parse helpers and been considering reducing those)

@Lucretiel
Copy link
Author

Lucretiel commented Feb 8, 2025

I thought (because of the one) that try_get_one returns the value for an option that takes a single argument; that is, it returns Some when --arg value, None when --arg is omitted entirely, and an error otherwise. I haven't been able to find any precise documentation about the underlying "data model" used by ArgMatches; how it's populated from a Command, and specifically how the various get_* and try_get_* methods interpret the stored data.

@epage
Copy link
Member

epage commented Feb 10, 2025

In clap v3, flags were a special case without a value in ArgMatches, instead users having to check for its presence. In clap v4, we transition to always setting a value in ArgMatches, hence the names like SetTrue and SetFalse for flag ArgAction.

In fact, SetTrue is just a convenience helper for Set that implies

arg
  .value_parser(BoolValueParser::new())
  .default_missing_value("true")
  .default_value("false")
  .num_args(0)

so get_flag is a helper for get_one (used for Set) which is a helper for get_many (used for Append, num_args)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants