default_value_if should work with flags that don't take a value #3294
Labels
A-derive
Area: #[derive]` macro API
C-enhancement
Category: Raise on the bar on expectations
S-triage
Status: New; needs maintainer attention.
Please complete the following tasks
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 havedefault_value_ifs
stating that anymystery
orsuspense
argument gives the default value oftrue
, andtakes_value
is false. However,default_value_if
anddefault_value_ifs
seem to needtakes_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
, andtry_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 oftakes_value(false)
. If this is the best solution for now, I suggest adding a note to thetakes_value
docs such as: "Note that iftakes_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
The text was updated successfully, but these errors were encountered: