diff --git a/src/parse/validator.rs b/src/parse/validator.rs index 980a68b9b52..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(a.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\'" + ); +}