Skip to content

Commit

Permalink
Auto merge of #595 - kbknapp:issues-575,593, r=kbknapp
Browse files Browse the repository at this point in the history
Issues 575,593
  • Loading branch information
homu committed Jul 23, 2016
2 parents 1b99091 + 625cbbc commit 8830dc4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,19 +1606,21 @@ impl<'a, 'b> Parser<'a, 'b>
}
}
} else if let Some(ru) = a.required_unless() {
let mut found_any = false;
for n in ru.iter() {
if matcher.contains(n) ||
self.groups
.get(n)
.map_or(false, |g| g.args.iter().any(|an| matcher.contains(an))) {
if !a.is_set(ArgSettings::RequiredUnlessAll) {
return true;
}
} else {
return false;
}
if !a.is_set(ArgSettings::RequiredUnlessAll) {
return true;
}
found_any = true;
} else if a.is_set(ArgSettings::RequiredUnlessAll) {
return false;
}
}
return true;
return found_any;
}
false
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ impl FromStr for AppSettings {
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
match &*s.to_ascii_lowercase() {
"subcommandsnegatereqs" => Ok(AppSettings::SubcommandsNegateReqs),
"subcommandsrequired" => Ok(AppSettings::SubcommandRequired),
"subcommandrequired" => Ok(AppSettings::SubcommandRequired),
"argrequiredelsehelp" => Ok(AppSettings::ArgRequiredElseHelp),
"globalversion" => Ok(AppSettings::GlobalVersion),
"versionlesssubcommands" => Ok(AppSettings::VersionlessSubcommands),
Expand Down Expand Up @@ -621,7 +621,7 @@ mod test {
fn app_settings_fromstr() {
assert_eq!("subcommandsnegatereqs".parse::<AppSettings>().unwrap(),
AppSettings::SubcommandsNegateReqs);
assert_eq!("subcommandsrequired".parse::<AppSettings>().unwrap(),
assert_eq!("subcommandrequired".parse::<AppSettings>().unwrap(),
AppSettings::SubcommandRequired);
assert_eq!("argrequiredelsehelp".parse::<AppSettings>().unwrap(),
AppSettings::ArgRequiredElseHelp);
Expand Down
23 changes: 23 additions & 0 deletions tests/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,29 @@ fn required_unless_one_2() {
assert!(!m.is_present("cfg"));
}

#[test]
fn required_unless_one_1() {
let res = App::new("unlessone")
.arg(Arg::with_name("cfg")
.required_unless_one(&["dbg", "infile"])
.takes_value(true)
.long("config"))
.arg(Arg::with_name("dbg")
.long("debug"))
.arg(Arg::with_name("infile")
.short("i")
.takes_value(true))
.get_matches_from_safe(vec![
"unlessone", "--debug"
]);

assert!(res.is_ok());
let m = res.unwrap();
assert!(!m.is_present("infile"));
assert!(!m.is_present("cfg"));
assert!(m.is_present("dbg"));
}

#[test]
fn required_unless_one_err() {
let res = App::new("unlessone")
Expand Down

0 comments on commit 8830dc4

Please sign in to comment.