Skip to content

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

@lefth

Description

@lefth

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-deriveArea: #[derive]` macro APIC-enhancementCategory: Raise on the bar on expectationsS-triageStatus: New; needs maintainer attention.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions