Added tests and fixed display of conflicted flags

This commit is contained in:
Julian Laubstein 2020-03-08 17:10:59 +01:00
parent 444bce2471
commit c34a0fdae9
2 changed files with 54 additions and 2 deletions

View file

@ -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(),
));

52
tests/arg_conflicts.rs Normal file
View file

@ -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\'"
);
}