Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
MarcelRobitaille opened this issue Nov 24, 2021 · 0 comments
Open
2 tasks done
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing

Comments

@MarcelRobitaille
Copy link

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

@epage epage added C-enhancement Category: Raise on the bar on expectations A-parsing Area: Parser's logic and needs it changed somehow. S-waiting-on-decision Status: Waiting on a go/no-go before implementing and removed T: new feature labels Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing
Projects
None yet
Development

No branches or pull requests

2 participants