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

default_value_if should work with flags that don't take a value #3294

Closed
2 tasks done
lefth opened this issue Jan 15, 2022 · 1 comment · Fixed by #3775
Closed
2 tasks done

default_value_if should work with flags that don't take a value #3294

lefth opened this issue Jan 15, 2022 · 1 comment · Fixed by #3775
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-triage Status: New; needs maintainer attention.

Comments

@lefth
Copy link

lefth commented Jan 15, 2022

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

3.0.7

Describe your use case

I want to have various specific flags imply a general flag, for example --mystery or --suspense implies --fiction. I don't want to require an extra "=true" on the flags.

Describe the solution you'd like

Clap can almost support this use case: fiction should have default_value_ifs stating that any mystery or suspense argument gives the default value of true, and takes_value is false. However, default_value_if and default_value_ifs seem to need takes_value. They set it implicitly, and when it is turned off, the implication does not happen.

Or if the right answer is to use min_value(0), that should be documented (see below).

Alternatives, if applicable

An alternative solution is to override parse, parse_from, try_parse, and try_parse_from (and call <Self as Parser>::*parse*) and call a validation function to set implied values.

Another alternative solution would be to let the struct or app define a post-parse hook (the validation function mentioned above).

If "true" and "false" are not valid positional values, the easiest workaround is to use min_values(0) instead of takes_value(false). If this is the best solution for now, I suggest adding a note to the takes_value docs such as: "Note that if takes_value(false) cannot be used due to a default value being set by another argument's presence, min_values(0) is a reasonable substitute."

Additional Context

This issue is shown in rust playground here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4855a88381f65cef8d07f7eab4d41e78

@lefth lefth added the C-enhancement Category: Raise on the bar on expectations label Jan 15, 2022
@epage
Copy link
Member

epage commented Jan 17, 2022

So there are ways around this in the builder API but this was reported against the derive API.

I have been considering re-doing flags so that they set a value, rather than relying on them being preset or not which would resolve this (and simplify several other APIs).

@epage epage added A-derive Area: #[derive]` macro API S-triage Status: New; needs maintainer attention. labels Jan 17, 2022
epage added a commit to epage/clap that referenced this issue Jun 1, 2022
Now that we can store constants for flags, we can apply defaults for
flags too.

Fixes clap-rs#3294
epage added a commit to epage/clap that referenced this issue Jun 1, 2022
Now that we can store constants for flags, we can apply defaults for
flags too.

Fixes clap-rs#3294
epage added a commit to epage/clap that referenced this issue Jun 1, 2022
Now that we can store constants for flags, we can apply defaults for
flags too.

Fixes clap-rs#3294
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-triage Status: New; needs maintainer attention.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants