epage / clapng

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
0 stars 0 forks source link

ArgMatches method to get the alias used (and long or short version) #244

Open epage opened 2 years ago

epage commented 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


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 to value_of that would return this.

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");

Alternatives, if applicable

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"
);

Additional Context

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".