Skip to content

Commit

Permalink
fix: Ensure we validate required-unless
Browse files Browse the repository at this point in the history
Found this when digging into #3197
  • Loading branch information
epage committed Dec 22, 2021
1 parent 90d4a3c commit 2e73be4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parse/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/builder/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2e73be4

Please sign in to comment.