Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

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

Open
2 tasks done
epage opened this issue Dec 6, 2021 · 0 comments
Open
2 tasks done

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

epage opened this issue Dec 6, 2021 · 0 comments

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by MarcelRobitaille
Wednesday Nov 24, 2021 at 18:25 GMT
Originally opened as clap-rs/clap#3048


Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant