Skip to content

Commit

Permalink
fix(Global Args): global arguments propogate fully now
Browse files Browse the repository at this point in the history
Closes #137
  • Loading branch information
kbknapp committed Jun 6, 2015
1 parent 245c218 commit 8f2c016
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
/// # .get_matches();
/// ```
pub fn arg(mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.add_arg(a);
self
}

fn add_arg(&mut self, a: Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>) {
if self.flags.contains_key(a.name) ||
self.opts.contains_key(a.name) ||
self.positionals_name.contains_key(a.name) {
Expand Down Expand Up @@ -635,7 +640,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
self.global_args.push(a);
}
self
}

/// Adds multiple arguments to the list of valid possibilties by iterating over a Vec of Args
Expand Down Expand Up @@ -824,14 +828,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
/// // Additional subcommand configuration goes here, such as other arguments...
/// # .get_matches();
/// ```
pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
pub fn subcommand(mut self, subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
if subcmd.name == "help" { self.needs_subcmd_help = false; }
{
for a in self.global_args.iter() {
subcmd = subcmd.arg(a.into());
}
}
self.subcommands.insert(subcmd.name.clone(), subcmd);
self
}
Expand Down Expand Up @@ -1317,9 +1316,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
// the real parsing function for subcommands
pub fn get_matches(mut self) -> ArgMatches<'ar, 'ar> {
self.verify_positionals();
for (_,sc) in self.subcommands.iter_mut() {
sc.verify_positionals();
}
self.propogate_globals();

let mut matches = ArgMatches::new();

Expand Down Expand Up @@ -1379,6 +1376,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
}

fn propogate_globals(&mut self) {
for (_,sc) in self.subcommands.iter_mut() {
{
for a in self.global_args.iter() {
sc.add_arg(a.into());
}
}
sc.verify_positionals();
}
}

/// Returns a suffix that can be empty, or is the standard 'did you mean phrase
fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle)
-> (String, Option<&'z str>)
Expand Down

0 comments on commit 8f2c016

Please sign in to comment.