Merge pull request #3205 from epage/unless

fix: Ensure we validate required-unless
This commit is contained in:
Ed Page 2021-12-23 08:47:23 -06:00 committed by GitHub
commit ad127fcc53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View file

@ -37,6 +37,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
if let ParseState::Opt(a) = parse_state {
debug!("Validator::validate: needs_val_of={:?}", a);
self.validate_required(matcher)?;
self.validate_required_unless(matcher)?;
let o = &self.p.app[&a];
reqs_validated = true;

View file

@ -258,6 +258,23 @@ fn required_unless_err() {
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}
#[test]
fn required_unless_present_with_optional_value() {
let res = App::new("unlesstest")
.arg(Arg::new("opt").long("opt").min_values(0).max_values(1))
.arg(
Arg::new("cfg")
.required_unless_present("dbg")
.takes_value(true)
.long("config"),
)
.arg(Arg::new("dbg").long("debug"))
.try_get_matches_from(vec!["unlesstest", "--opt"]);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}
// REQUIRED_UNLESS_ALL
#[test]