Skip to content

Commit

Permalink
fix: Derive display order after propagation
Browse files Browse the repository at this point in the history
Don't attempt to change the display order of flags/options until any app
settings have been propagated down from a parent App in case DeriveDisplayOrder
and/or UnifiedHelpMessage are propagated.

Fixes #706
  • Loading branch information
Nemo157 committed Oct 26, 2016
1 parent f5b577c commit 9cb6fac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@ impl<'a, 'b> App<'a, 'b> {
// before parsing incase we run into a subcommand
self.p.propogate_globals();
self.p.propogate_settings();
self.p.derive_display_order();

let mut matcher = ArgMatcher::new();

Expand Down
37 changes: 20 additions & 17 deletions src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,11 @@ impl<'a, 'b> Parser<'a, 'b>
self.positionals.insert(i, pb);
} else if a.is_set(ArgSettings::TakesValue) {
let mut ob = OptBuilder::from_arg(a, &mut self.required);
if self.settings.is_set(AppSettings::DeriveDisplayOrder) && a.disp_ord == 999 {
ob.disp_ord = if self.settings.is_set(AppSettings::UnifiedHelpMessage) {
self.flags.len() + self.opts.len()
} else {
self.opts.len()
};
}
ob.unified_ord = self.flags.len() + self.opts.len();
self.opts.push(ob);
} else {
let mut fb = FlagBuilder::from(a);
if self.settings.is_set(AppSettings::DeriveDisplayOrder) && a.disp_ord == 999 {
fb.disp_ord = if self.settings.is_set(AppSettings::UnifiedHelpMessage) {
self.flags.len() + self.opts.len()
} else {
self.flags.len()
};
}
fb.unified_ord = self.flags.len() + self.opts.len();
self.flags.push(fb);
}
if a.is_set(ArgSettings::Global) {
Expand Down Expand Up @@ -242,9 +230,6 @@ impl<'a, 'b> Parser<'a, 'b>
sdebugln!("No");
}

if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
subcmd.p.meta.disp_ord = self.subcommands.len();
}
self.subcommands.push(subcmd);
}

Expand Down Expand Up @@ -275,6 +260,24 @@ impl<'a, 'b> Parser<'a, 'b>
}
}

pub fn derive_display_order(&mut self) {
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
let unified = self.settings.is_set(AppSettings::UnifiedHelpMessage);
for (i, o) in self.opts.iter_mut().enumerate().filter(|&(_, ref o)| o.disp_ord == 999) {
o.disp_ord = if unified { o.unified_ord } else { i };
}
for (i, f) in self.flags.iter_mut().enumerate().filter(|&(_, ref f)| f.disp_ord == 999) {
f.disp_ord = if unified { f.unified_ord } else { i };
}
for (i, sc) in &mut self.subcommands.iter_mut().enumerate().filter(|&(_, ref sc)| sc.p.meta.disp_ord == 999) {
sc.p.meta.disp_ord = i;
}
}
for sc in &mut self.subcommands {
sc.p.derive_display_order();
}
}

pub fn required(&self) -> Iter<&str> {
self.required.iter()
}
Expand Down
4 changes: 4 additions & 0 deletions src/args/arg_builder/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct FlagBuilder<'n, 'e> {
pub overrides: Option<Vec<&'e str>>,
pub settings: ArgFlags,
pub disp_ord: usize,
pub unified_ord: usize,
}

impl<'n, 'e> Default for FlagBuilder<'n, 'e> {
Expand All @@ -40,6 +41,7 @@ impl<'n, 'e> Default for FlagBuilder<'n, 'e> {
overrides: None,
settings: ArgFlags::new(),
disp_ord: 999,
unified_ord: 999,
}
}
}
Expand Down Expand Up @@ -77,6 +79,7 @@ impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> {
requires: a.requires.clone(),
settings: a.settings,
disp_ord: a.disp_ord,
..Default::default()
}
}
}
Expand Down Expand Up @@ -106,6 +109,7 @@ impl<'n, 'e> Clone for FlagBuilder<'n, 'e> {
requires: self.requires.clone(),
settings: self.settings,
disp_ord: self.disp_ord,
unified_ord: self.unified_ord,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct OptBuilder<'n, 'e> {
pub val_delim: Option<char>,
pub default_val: Option<&'n str>,
pub disp_ord: usize,
pub unified_ord: usize,
pub r_unless: Option<Vec<&'e str>>,
}

Expand All @@ -55,6 +56,7 @@ impl<'n, 'e> Default for OptBuilder<'n, 'e> {
val_delim: Some(','),
default_val: None,
disp_ord: 999,
unified_ord: 999,
r_unless: None,
}
}
Expand Down Expand Up @@ -177,6 +179,7 @@ impl<'n, 'e> Clone for OptBuilder<'n, 'e> {
requires: self.requires.clone(),
settings: self.settings,
disp_ord: self.disp_ord,
unified_ord: self.unified_ord,
num_vals: self.num_vals,
min_vals: self.min_vals,
max_vals: self.max_vals,
Expand Down

0 comments on commit 9cb6fac

Please sign in to comment.