Open hasezoey opened 3 months ago
There are attributes that are implied by the data type, see https://docs.rs/clap/latest/clap/_derive/index.html#arg-types and if you aren't using the data type in the built-in way, then you have to override those. In this case, you need to set action = ArgAction::Set
.
and if you aren't using the data type in the built-in way, then you have to override those. In this case, you need to set action = ArgAction::Set.
to my knowledge i am using data type bool
, which is documented as .action(ArgAction::SetTrue)
, and ArgAction::SetTrue
has documented
No value is allowed. To optionally accept a value, see Arg::default_missing_value
and default_missing_value
writes:
NOTE: using this configuration option requires the use of the .num_args(0..N) and the .require_equals(true) configuration option. These are required in order to unambiguously determine what, if any, value was supplied for the argument.
and also a example:
fn cli() -> Command {
Command::new("prog")
.arg(Arg::new("create").long("create")
.value_name("BOOL")
.value_parser(value_parser!(bool))
.num_args(0..=1)
.require_equals(true)
.default_missing_value("true")
)
}
Note: i just discovered default_missing_value
has a example for bools which has .value_parser(value_parser!(bool))
set, is this something i would also need to do in derive?
I have now tested using action = ArgAction::Set
, and yes this resolves the error. Maybe some documentation updates would be good?
The discrepancy is Builder vs Derive APIs. The Builder API defaults to Set
unconditionally while the Derive API tries to be a little smarter.
I've tried to avoid as much talk about the Derive API in the Builder API. Part of this was from a negative experience as a user when clap had the Builder, YAML, and Usage APIs and each entry point was documented with the schema for them which made the docs more bloated and harder to follow.
I've tried to avoid as much talk about the Derive API in the Builder API.
i can understand that, so maybe add it as a note when default_missing_value
gets mentioned in the derive API (as it currently is not listed there)?
default_missing_value
is covered by a blanket statement on raw attributes. Its not great but neither are the alternatives. #4090 is for exploring ways of improving that.
Please complete the following tasks
Rust Version
rustc 1.76.0 (07dca489a 2024-02-04)
Clap Version
4.5.3
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run -- --test=false
Actual Behaviour
Panic:
Expected Behaviour
No panic, as per documentation
default_missing_value
.Additional Context
i know that
default_missing_value
is currently not documented in derive attributes, but i thought it would work anyway.in case it is not clear, i am trying to do the following without problems:
Debug Output