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

A require* method that expressing and(&&) condition when requesting an argument #2326

Closed
2 tasks done
zzz845zz opened this issue Feb 4, 2021 · 1 comment · Fixed by #2371
Closed
2 tasks done

A require* method that expressing and(&&) condition when requesting an argument #2326

zzz845zz opened this issue Feb 4, 2021 · 1 comment · Fixed by #2371
Labels
Milestone

Comments

@zzz845zz
Copy link

zzz845zz commented Feb 4, 2021

Make sure you completed the following tasks

Describe your use case

I think the function that requires some arguments only if multiple conditions are satisfied is helpful.

The example of required_ifs below is similar to or(||) expression, not and(&&) (from here).

  • In this example, cfg is required when --extra=val or --option=spec
  • I want a way to require cfg when --extra=val and --option=spec
let res = App::new("prog")
    .arg(Arg::with_name("cfg")
        .required_ifs(&[
            ("extra", "val"),
            ("option", "spec")
        ])
        .takes_value(true)
        .long("config"))
    .arg(Arg::with_name("extra")
        .takes_value(true)
        .long("extra"))
    .arg(Arg::with_name("option")
        .takes_value(true)
        .long("option"))
    .get_matches_from_safe(vec![
        "prog", "--option", "other"
    ]);

assert!(res.is_ok()); // We didn't use --option=spec, or --extra=val so "cfg" isn't required

For more simply, the case above likes

if (extra=="val" || option=="spec") {
    // use cfg!!
}

But what i want is the example below

if (extra=="val" && option=="spec") {
    // use cfg!!
} 

Additional context

Related to #2323

@pksunkara
Copy link
Member

The name should be require_if_eq_all

@pksunkara pksunkara added this to the 3.1 milestone Feb 4, 2021
omar25h added a commit to omar25h/clap that referenced this issue Feb 27, 2021
omar25h added a commit to omar25h/clap that referenced this issue Feb 27, 2021
omar25h added a commit to omar25h/clap that referenced this issue Feb 27, 2021
omar25h added a commit to omar25h/clap that referenced this issue Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants