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

Get which arg of mutually exclusive ArgGroup was passed #177

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

Get which arg of mutually exclusive ArgGroup was passed #177

epage opened this issue Dec 6, 2021 · 7 comments

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by Qyriad
Thursday Jan 28, 2021 at 20:44 GMT
Originally opened as clap-rs/clap#2317


Make sure you completed the following tasks

Describe your use case

One of the use cases the documentation lists for ArgGroup is to have a set of mutually exclusive arguments (with .multiple(false)), but the only way to get which of those arguments was passed is to call matches.is_present(name) for every argument in that group. It would be really nice if I could just get which argument of a mutually exclusive argument group was passed. Currently, to do this you have to check each argument individually, doing something like:

let directions = &["left", "right", "forward"];

let matches = App::new("ArgGroup test")
    .arg(Arg::with_name("left")
        .short("-l")
        .long("--left"))
    .arg(Arg::with_name("right")
        .short("-r")
        .long("--right"))
    .arg(Arg::with_name("forward")
        .short("-f")
        .long("--forward"))
    .group(ArgGroup::with_name("direction")
        .required(true)
        .multiple(false)
        .args(directions))
    .get_matches();

let mut passed_direction = None;
for direction in directions {
    if matches.is_present(direction) {
        passed_direction = Some(direction);
    }
}
let passed_direction = passed_direction.unwrap();

Which is kind of tedious, and not very clean.

Describe the solution you'd like

Being able to write something like:

let matches = App::new("ArgGroup test")
    .arg(Arg::with_name("left")
        .short("-l")
        .long("--left"))
    .arg(Arg::with_name("right")
        .short("-r")
        .long("--right"))
    .arg(Arg::with_name("forward")
        .short("-f")
        .long("--forward"))
    .group(ArgGroup::with_name("direction")
        .required(true)
        .multiple(false)
        .args(&["left", "right", "forward"]))
    .get_matches();

let passed_direction = matches.value_of("direction");

would be really nice. value_of() is just an example, there — I'm not particular about the exact API is to get this 🙂

@epage epage added this to the 3.1 milestone Dec 6, 2021
@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by ldm0
Friday Jan 29, 2021 at 16:44 GMT


I think conflicts_with_all might matches your use case.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Friday Jan 29, 2021 at 18:05 GMT


But that's not the thing he is asking for.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by Qyriad
Friday Jan 29, 2021 at 18:39 GMT


she*, but I believe you're correct. ArgGroup::multiple() already sets the mutual exclusivity. I'm looking for a way to get which argument in an ArgGroup was passed, from an ArgMatches. I don't see how conflicts_with_all() helps me there.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by ldm0
Saturday Jan 30, 2021 at 04:21 GMT


Sorry for disturbing, haven't slept much yesterday and my mind isn't clear.

So we need to design a new api specifically for fetching the triggered argument and also the value? @pksunkara .

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Saturday Jan 30, 2021 at 10:30 GMT


Yes, but this is not a priority for 3.0

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by Qyriad
Saturday Jan 30, 2021 at 15:54 GMT


So we need to design a new api specifically for fetching the triggered argument and also the value?

Ideally, though I would also be fine with an API just for fetching the triggered argument — which would mean that getting the value of that argument would be a separate step.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by DocKDE
Tuesday Oct 26, 2021 at 18:15 GMT


I'd like this as well but I'll be patient :)

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