Repository navigation
conflicts_if_eq_any/conflicts_if_eq_all #6478
|
Hello. I searched issues for these non-existing method names, and found no immediate results. But I'm sure this came up before. use clap::{ValueEnum, Parser};
#[derive(ValueEnum, Clone, Debug)]
enum Foo {
A,
B,
C,
D,
}
#[derive(Parser, Debug)]
struct Cli {
#[clap(long)]
foo: Foo,
#[clap(long,
required_if_eq_any([
("foo", "a"),
("foo", "b"),
]),
conflicts_if_eq_any([
("foo", "c"),
("foo", "d"),
]),
)]
foo_info: Option<String>,
}
fn main() {
dbg!{Cli::parse()};
}Is the conflicts part achievable in some other clean way that exists today? |
Replies: 1 comment 2 replies
|
There's no conflicts_if_eq* in clap right now. Only the required side has value-conditional variants (required_if_eq_any, requires_if); the conflict methods, conflicts_with and conflicts_with_all, are both unconditional. Cleanest thing today is to keep the required side declarative and do the conflict as a small post-parse check that raises a real clap error: #[clap(long, required_if_eq_any([("foo", "a"), ("foo", "b")]))]
foo_info: Option<String>,
// ...after parse:
if matches!(cli.foo, Foo::C | Foo::D) && cli.foo_info.is_some() {
let mut cmd = Cli::command();
cmd.error(ErrorKind::ArgumentConflict,
"--foo-info can't be used with --foo c or d").exit();
}On clap 4.6.6 that gives you the behavior you want: |
There are a lot of different options people want for required, requires, conflicts, overrides, etc. To support them all would run into issues with a bloated API, compile times, and binary size.
We'd like to explore how to make this more extensible so people pay for what they use but haven't had time to drive it. See also #3476