Fixes arg having required_unless on default values

This commit is contained in:
Pavan Kumar Sunkara 2021-10-26 20:49:21 +01:00
parent a88562c12b
commit ac1de1fc78
No known key found for this signature in database
GPG key ID: 7149B807A81DE336
2 changed files with 36 additions and 3 deletions

View file

@ -69,10 +69,11 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
));
}
self.validate_conflicts(matcher)?;
if !(self.p.is_set(AS::SubcommandsNegateReqs) && is_subcmd || reqs_validated) {
self.validate_required(matcher)?;
self.validate_required_unless(matcher)?;
}
self.validate_matched_args(matcher)?;
Ok(())
@ -583,6 +584,9 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
return self.missing_required_error(matcher, vec![a.id.clone()]);
}
}
self.validate_required_unless(matcher)?;
Ok(())
}
@ -619,6 +623,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
})
.map(|a| a.id.clone())
.collect();
if failed_args.is_empty() {
Ok(())
} else {
@ -631,10 +636,14 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
debug!("Validator::fails_arg_required_unless: a={:?}", a.name);
if a.is_set(ArgSettings::RequiredUnlessAll) {
debug!("Validator::fails_arg_required_unless:{}:All", a.name);
!a.r_unless.iter().all(|id| matcher.contains(id))
!a.r_unless
.iter()
.all(|id| matcher.contains(id) && !matcher.is_default_value(id))
} else {
debug!("Validator::fails_arg_required_unless:{}:Any", a.name);
!a.r_unless.iter().any(|id| matcher.contains(id))
!a.r_unless
.iter()
.any(|id| matcher.contains(id) && !matcher.is_default_value(id))
}
}

View file

@ -1196,3 +1196,27 @@ fn required_if_eq_all_on_default_value() {
assert_eq!(m.value_of("opt"), Some("default"));
assert!(!m.is_present("flag"));
}
#[test]
fn required_unless_on_default_value() {
let result = App::new("prog")
.arg(Arg::new("opt").long("opt").default_value("default"))
.arg(Arg::new("flag").long("flag").required_unless_present("opt"))
.try_get_matches_from(vec!["myprog"]);
assert!(result.is_err(), "{:?}", result.unwrap());
}
#[test]
fn required_unless_all_on_default_value() {
let result = App::new("prog")
.arg(Arg::new("opt").long("opt").default_value("default"))
.arg(
Arg::new("flag")
.long("flag")
.required_unless_present_all(&["opt"]),
)
.try_get_matches_from(vec!["myprog"]);
assert!(result.is_err(), "{:?}", result.unwrap());
}