Skip to content

Commit

Permalink
fix(App, Args): fixed subcommand reqs negation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinatorul committed Aug 24, 2015
1 parent 35114c9 commit 0efcbb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2391,17 +2391,19 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self.print_help();
self.exit(1);
}
if (!self.subcmds_neg_reqs) && self.validate_required(&matches) {
self.report_error(format!("The following required arguments were not \
supplied:{}",
self.get_required_from(self.required.iter()
.map(|s| *s)
.collect::<Vec<_>>())
.iter()
.fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",
Format::Error(s))[..])),
true,
Some(matches.args.keys().map(|k| *k).collect()));
if (!self.subcmds_neg_reqs) || matches.subcommand_name().is_none() {
if self.validate_required(&matches) {
self.report_error(format!("The following required arguments were not \
supplied:{}",
self.get_required_from(self.required.iter()
.map(|s| *s)
.collect::<Vec<_>>())
.iter()
.fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",
Format::Error(s))[..])),
true,
Some(matches.args.keys().map(|k| *k).collect()));
}
}
if matches.args.is_empty() && matches.subcommand_name().is_none() && self.help_on_no_args {
self.print_help();
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod fmt;

#[cfg(test)]
mod tests {
use super::{App, Arg, SubCommand};
use super::{App, Arg, SubCommand, AppSettings};
use std::collections::HashSet;
use std::vec::Vec;

Expand Down Expand Up @@ -1255,4 +1255,16 @@ mod tests {
App::new("short_flag")
.arg(Arg::from_usage("-f 'some flag'"));
}

#[test]
fn sub_command_negate_requred() {
App::new("sub_command_negate")
.setting(AppSettings::SubcommandsNegateReqs)
.arg(Arg::with_name("test")
.required(true)
.index(1))
.subcommand(SubCommand::with_name("sub1"))
.subcommand(SubCommand::with_name("sub1"))
.get_matches_from(vec!["", "sub1"]);
}
}

0 comments on commit 0efcbb6

Please sign in to comment.