From 444bce247138b6b57a2a5f0e9035fedb2db26b6c Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sun, 8 Mar 2020 16:39:14 +0100 Subject: [PATCH 1/2] Fixed #1622 --- src/parse/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parse/validator.rs b/src/parse/validator.rs index 980a68b9b52..c05e56cddd7 100644 --- a/src/parse/validator.rs +++ b/src/parse/validator.rs @@ -177,7 +177,7 @@ impl<'b, 'c, 'z> Validator<'b, 'c, 'z> { if v.contains(&name) { return Err(Error::argument_conflict( a, - Some(a.to_string()), + Some(name.to_string()), &*usg, self.p.app.color(), )); From c34a0fdae9b0606dae0ac4c31271bbaf0c2ad619 Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sun, 8 Mar 2020 17:10:59 +0100 Subject: [PATCH 2/2] Added tests and fixed display of conflicted flags --- src/parse/validator.rs | 4 ++-- tests/arg_conflicts.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/arg_conflicts.rs diff --git a/src/parse/validator.rs b/src/parse/validator.rs index c05e56cddd7..f310f84c877 100644 --- a/src/parse/validator.rs +++ b/src/parse/validator.rs @@ -170,14 +170,14 @@ impl<'b, 'c, 'z> Validator<'b, 'c, 'z> { fn build_conflict_err(&self, name: Id, matcher: &ArgMatcher) -> ClapResult<()> { debugln!("build_err!: name={}", name); let usg = Usage::new(self.p).create_usage_with_title(&[]); - if self.p.app.find(name).is_some() { + if let Some(other_arg) = self.p.app.find(name) { for &k in matcher.arg_names() { if let Some(a) = self.p.app.find(k) { if let Some(ref v) = a.blacklist { if v.contains(&name) { return Err(Error::argument_conflict( a, - Some(name.to_string()), + Some(other_arg.to_string()), &*usg, self.p.app.color(), )); diff --git a/tests/arg_conflicts.rs b/tests/arg_conflicts.rs new file mode 100644 index 00000000000..28acb97f8e5 --- /dev/null +++ b/tests/arg_conflicts.rs @@ -0,0 +1,52 @@ +use clap::{App, Arg}; + +#[test] +fn two_conflicting_arguments() { + let a = App::new("two_conflicting_arguments") + .arg( + Arg::with_name("develop") + .long("develop") + .conflicts_with("production"), + ) + .arg( + Arg::with_name("production") + .long("production") + .conflicts_with("develop"), + ) + .try_get_matches_from(vec!["", "--develop", "--production"]); + + assert!(a.is_err()); + let a = a.unwrap_err(); + assert_eq!( + a.cause, + "The argument \'--develop\' cannot be used with \'--production\'" + ); +} + +#[test] +fn three_conflicting_arguments() { + let a = App::new("two_conflicting_arguments") + .arg( + Arg::with_name("one") + .long("one") + .conflicts_with_all(&["two", "three"]), + ) + .arg( + Arg::with_name("two") + .long("two") + .conflicts_with_all(&["one", "three"]), + ) + .arg( + Arg::with_name("three") + .long("three") + .conflicts_with_all(&["one", "two"]), + ) + .try_get_matches_from(vec!["", "--one", "--two", "--three"]); + + assert!(a.is_err()); + let a = a.unwrap_err(); + assert_eq!( + a.cause, + "The argument \'--one\' cannot be used with \'--two\'" + ); +}