Skip to content

Commit

Permalink
Added tests and fixed display of conflicted flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sphinxc0re committed Mar 8, 2020
1 parent 444bce2 commit c34a0fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parse/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
Expand Down
52 changes: 52 additions & 0 deletions tests/arg_conflicts.rs
Original file line number Diff line number Diff line change
@@ -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\'"
);
}

0 comments on commit c34a0fd

Please sign in to comment.