Skip to content

Commit b41afa8

Browse files
Vinatorulkbknapp
authored andcommitted
fix(App, Args): fixed subcommand reqs negation
Closes #188
1 parent 35114c9 commit b41afa8

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
23912391
self.print_help();
23922392
self.exit(1);
23932393
}
2394-
if (!self.subcmds_neg_reqs) && self.validate_required(&matches) {
2394+
if ((!self.subcmds_neg_reqs) || matches.subcommand_name().is_none()) && self.validate_required(&matches) {
23952395
self.report_error(format!("The following required arguments were not \
23962396
supplied:{}",
23972397
self.get_required_from(self.required.iter()

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod fmt;
2727

2828
#[cfg(test)]
2929
mod tests {
30-
use super::{App, Arg, SubCommand};
30+
use super::{App, Arg, SubCommand, AppSettings};
3131
use std::collections::HashSet;
3232
use std::vec::Vec;
3333

@@ -1255,4 +1255,16 @@ mod tests {
12551255
App::new("short_flag")
12561256
.arg(Arg::from_usage("-f 'some flag'"));
12571257
}
1258+
1259+
#[test]
1260+
fn sub_command_negate_requred() {
1261+
App::new("sub_command_negate")
1262+
.setting(AppSettings::SubcommandsNegateReqs)
1263+
.arg(Arg::with_name("test")
1264+
.required(true)
1265+
.index(1))
1266+
.subcommand(SubCommand::with_name("sub1"))
1267+
.subcommand(SubCommand::with_name("sub1"))
1268+
.get_matches_from(vec!["", "sub1"]);
1269+
}
12581270
}

0 commit comments

Comments
 (0)