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

How can I handle this case which requires any group #1537

Open
oblique opened this issue Aug 28, 2019 · 2 comments
Open

How can I handle this case which requires any group #1537

oblique opened this issue Aug 28, 2019 · 2 comments
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate 💸 $20

Comments

@oblique
Copy link

oblique commented Aug 28, 2019

I have a special flag which when it is set I want always one of two groups to match. This special flag should never passed alone.

To be more clear I created a simple example on what I want to achieve:

use clap::{App, Arg, ArgGroup};

fn main() {
    let mut app = App::new("my-app")
        .arg(Arg::with_name("special").long("special"))
        .arg(Arg::with_name("opt-a1").long("opt-a1"))
        .arg(Arg::with_name("opt-a2").long("opt-a2"))
        .arg(Arg::with_name("opt-b1").long("opt-b1"))
        .arg(Arg::with_name("opt-b2").long("opt-b2"))
        .group(
            ArgGroup::with_name("special-and-opt-a")
                .args(&["opt-a1", "opt-a2"])
                .requires_all(&["special", "opt-a1", "opt-a2"])
                .conflicts_with("special-and-opt-b")
                .multiple(true)
                .required(false),
        )
        .group(
            ArgGroup::with_name("special-and-opt-b")
                .args(&["opt-b1", "opt-b2"])
                .requires_all(&["special", "opt-b1", "opt-b2"])
                .conflicts_with("special-and-opt-a")
                .multiple(true)
                .required(false),
        );

    let valid_cases = vec![
        vec!["my-app"],
        vec!["my-app", "--special", "--opt-a1", "--opt-a2"],
        vec!["my-app", "--special", "--opt-b1", "--opt-b2"],
    ];

    let invalid_cases = vec![
        vec!["my-app", "--opt-a1", "--opt-a2"],
        vec!["my-app", "--opt-b1", "--opt-b2"],
        vec!["my-app", "--special"],
        vec!["my-app", "--special", "--opt-a1"],
        vec!["my-app", "--special", "--opt-b1"],
        vec!["my-app", "--special", "--opt-a1", "--opt-a2", "--opt-b1"],
        vec!["my-app", "--special", "--opt-b1", "--opt-b2", "--opt-a1"],
        vec!["my-app", "--special", "--opt-a1", "--opt-a2", "--opt-b1", "-opt-b2"],
    ];

    for x in valid_cases {
        assert!(app.get_matches_from_safe_borrow(x).is_ok());
    }

    for x in invalid_cases {
        assert!(app.get_matches_from_safe_borrow(x).is_err());
    }
}

The case which I can not find how to handle is ["my-app", "--special"]. Of-course I can workaround this with is_present, but I'm curious if there is a solution via groups. Maybe this is a valid case where requires_any is needed.

@CreepySkeleton
Copy link
Contributor

maybe we need to introduce requires_any(&[group/option])?

@CreepySkeleton CreepySkeleton added A-parsing Area: Parser's logic and needs it changed somehow. P4: nice to have and removed cc: CreepySkeleton labels Feb 6, 2020
@CreepySkeleton CreepySkeleton added this to the 3.1 milestone Feb 6, 2020
@CreepySkeleton CreepySkeleton removed their assignment Feb 6, 2020
@pksunkara pksunkara removed the W: 3.x label Aug 13, 2021
@nrdxp
Copy link

nrdxp commented Aug 25, 2021

Just ran into this situation today, and could use a requires_any

@epage epage added C-enhancement Category: Raise on the bar on expectations A-validators Area: ArgMatches validation logi E-medium Call for participation: Experience needed to fix: Medium / intermediate and removed T: new feature A-parsing Area: Parser's logic and needs it changed somehow. labels Dec 8, 2021
@epage epage removed this from the 3.1 milestone Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate 💸 $20
Projects
None yet
Development

No branches or pull requests

5 participants