Skip to content

Commit

Permalink
fix(Settings): fixes an issue where settings weren't propogated down …
Browse files Browse the repository at this point in the history
…through grand-child subcommands

In some cases settings were only propogated down one level deep, this
commit ensures settings are propogated down through all subcommands
recursively.

Closes #638
  • Loading branch information
kbknapp committed Sep 5, 2016
1 parent 48bc038 commit b3efc10
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,10 @@ impl<'a, 'b> App<'a, 'b> {
{
// Verify all positional assertions pass
self.p.verify_positionals();
// If there are global arguments, we need to propgate them down to subcommands
// If there are global arguments, or settings we need to propgate them down to subcommands
// before parsing incase we run into a subcommand
self.p.propogate_globals();
self.p.propogate_settings();

let mut matcher = ArgMatcher::new();

Expand Down
47 changes: 27 additions & 20 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,32 +241,39 @@ impl<'a, 'b> Parser<'a, 'b>
sdebugln!("No");
}

debug!("Using Setting VersionlessSubcommands...");
if self.settings.is_set(AppSettings::VersionlessSubcommands) {
sdebugln!("Yes");
subcmd.p.settings.set(AppSettings::DisableVersion);
} else {
sdebugln!("No");
}
debug!("Using Setting GlobalVersion...");
if self.settings.is_set(AppSettings::GlobalVersion) && subcmd.p.meta.version.is_none() &&
self.meta.version.is_some() {
sdebugln!("Yes");
subcmd = subcmd.setting(AppSettings::GlobalVersion)
.version(self.meta.version.unwrap());
} else {
sdebugln!("No");
}
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
subcmd.p.meta.disp_ord = self.subcommands.len();
}
for s in &self.g_settings {
subcmd.p.set(*s);
subcmd.p.g_settings.push(*s);
}
self.subcommands.push(subcmd);
}

pub fn propogate_settings(&mut self) {
for sc in &mut self.subcommands {
// We have to create a new scope in order to tell rustc the borrow of `sc` is
// done and to recursively call this method
{
let vsc = self.settings.is_set(AppSettings::VersionlessSubcommands);
let gv = self.settings.is_set(AppSettings::GlobalVersion);

debugln!("VersionlessSubcommands set...{:?}", vsc);
debugln!("GlobalVersion set...{:?}", gv);

if vsc {
sc.p.settings.set(AppSettings::DisableVersion);
}
if gv && sc.p.meta.version.is_none() && self.meta.version.is_some() {
sc.p.set(AppSettings::GlobalVersion);
sc.p.meta.version = Some(self.meta.version.unwrap());
}
for s in &self.g_settings {
sc.p.set(*s);
sc.p.g_settings.push(*s);
}
}
sc.p.propogate_settings();
}
}

pub fn required(&self) -> Iter<&str> {
self.required.iter()
}
Expand Down

0 comments on commit b3efc10

Please sign in to comment.