Open epage opened 2 years ago
Issue by MarcelRobitaille Wednesday Nov 24, 2021 at 18:25 GMT Originally opened as https://github.com/clap-rs/clap/issues/3048
2.33.3
I am trying to determine which variant of an option was used. For example, did the user type --number or just -n.
--number
-n
I think ArgMatches should have a method similar to value_of that would return this.
ArgMatches
value_of
let matches = App::new("cli") .arg( Arg::with_name("number") .short("n") .long("number") .alias("limit") .takes_value(true), ) .get_matches_from(vec!["cli", "--limit", "10"]); assert_eq!(matches.variant_of("number").unwrap(), "--limit");
Here is my workaround.
use std::env; let matches = App::new("cli") .arg( Arg::with_name("number") .short("n") .long("number") .alias("limit") .takes_value(true), ) .get_matches(); assert_eq!( env::args() .nth(matches.index_of("number").unwrap() - 1) .unwrap(), "--limit" );
I am trying to print more helpful error messages. For example, instead of Failed to parse argument "number" I could print Failed to parse argument "--limit".
Failed to parse argument "number"
Failed to parse argument "--limit"
Please complete the following tasks
Clap Version
2.33.3
Describe your use case
I am trying to determine which variant of an option was used. For example, did the user type
--number
or just-n
.Describe the solution you'd like
I think
ArgMatches
should have a method similar tovalue_of
that would return this.Alternatives, if applicable
Here is my workaround.
Additional Context
I am trying to print more helpful error messages. For example, instead of
Failed to parse argument "number"
I could printFailed to parse argument "--limit"
.