Skip to content

Commit 8f2c016

Browse files
committed
fix(Global Args): global arguments propogate fully now
Closes #137
1 parent 245c218 commit 8f2c016

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/app.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
401401
/// # .get_matches();
402402
/// ```
403403
pub fn arg(mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
404+
self.add_arg(a);
405+
self
406+
}
407+
408+
fn add_arg(&mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) {
404409
if self.flags.contains_key(a.name) ||
405410
self.opts.contains_key(a.name) ||
406411
self.positionals_name.contains_key(a.name) {
@@ -635,7 +640,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
635640
}
636641
self.global_args.push(a);
637642
}
638-
self
639643
}
640644

641645
/// Adds multiple arguments to the list of valid possibilties by iterating over a Vec of Args
@@ -824,14 +828,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
824828
/// // Additional subcommand configuration goes here, such as other arguments...
825829
/// # .get_matches();
826830
/// ```
827-
pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
831+
pub fn subcommand(mut self, subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
828832
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
829833
if subcmd.name == "help" { self.needs_subcmd_help = false; }
830-
{
831-
for a in self.global_args.iter() {
832-
subcmd = subcmd.arg(a.into());
833-
}
834-
}
835834
self.subcommands.insert(subcmd.name.clone(), subcmd);
836835
self
837836
}
@@ -1317,9 +1316,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
13171316
// the real parsing function for subcommands
13181317
pub fn get_matches(mut self) -> ArgMatches<'ar, 'ar> {
13191318
self.verify_positionals();
1320-
for (_,sc) in self.subcommands.iter_mut() {
1321-
sc.verify_positionals();
1322-
}
1319+
self.propogate_globals();
13231320

13241321
let mut matches = ArgMatches::new();
13251322

@@ -1379,6 +1376,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
13791376
}
13801377
}
13811378

1379+
fn propogate_globals(&mut self) {
1380+
for (_,sc) in self.subcommands.iter_mut() {
1381+
{
1382+
for a in self.global_args.iter() {
1383+
sc.add_arg(a.into());
1384+
}
1385+
}
1386+
sc.verify_positionals();
1387+
}
1388+
}
1389+
13821390
/// Returns a suffix that can be empty, or is the standard 'did you mean phrase
13831391
fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle)
13841392
-> (String, Option<&'z str>)

0 commit comments

Comments
 (0)