Skip to content

Commit

Permalink
imp(Aliases): improves readability of asliases in help messages
Browse files Browse the repository at this point in the history
Aliases are now displayed after the help text inside a `[aliases: als1, als2, als3]` style box.

Closes #526
Closes #529
  • Loading branch information
kbknapp committed Jun 13, 2016
1 parent 50ca205 commit ca511de
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,17 @@ impl<'a> Help<'a> {
"".into()
}
});
} else if let Some(ref aliases) = a.aliases() {
debugln!("Writing aliases");
return format!(" [aliases: {}]",
if self.color {
aliases.iter()
.map(|v| format!("{}", self.cizer.good(v)))
.collect::<Vec<_>>()
.join(", ")
} else {
aliases.join(", ")
});
} else if !self.hide_pv {
debugln!("Writing values");
if let Some(ref pv) = a.possible_vals() {
Expand Down Expand Up @@ -548,21 +559,6 @@ impl<'a> Help<'a> {

let mut ord_m = VecMap::new();
for sc in parser.subcommands.iter().filter(|s| !s.p.is_set(AppSettings::Hidden)) {
let sc = if let Some(ref aliases) = sc.p.meta.aliases {
let mut a = App::new(format!("{}|{}", &*sc.p.meta.name, aliases.iter()
.filter(|&&(_, vis)| vis)
.map(|&(n, _)| n)
.collect::<Vec<_>>()
.join("|")));
a = if let Some(about) = sc.p.meta.about {
a.about(about)
} else {
a
};
a
} else {
sc.clone()
};
let btm = ord_m.entry(sc.p.meta.disp_ord).or_insert(BTreeMap::new());
longest = cmp::max(longest, sc.p.meta.name.len());
btm.insert(sc.p.meta.name.clone(), sc.clone());
Expand Down
12 changes: 12 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,18 @@ impl<'n, 'e> AnyArg<'n, 'e> for App<'n, 'e> {
fn longest_filter(&self) -> bool {
true
}
fn aliases(&self) -> Option<Vec<&'e str>> {
if let Some(ref aliases) = self.p.meta.aliases {
let vis_aliases: Vec<_> = aliases.iter().filter_map(|&(n,v)| if v { Some(n) } else {None}).collect();
if vis_aliases.is_empty() {
None
} else {
Some(vis_aliases)
}
} else {
None
}
}
}

impl<'n, 'e> fmt::Display for App<'n, 'e> {
Expand Down
1 change: 1 addition & 0 deletions src/args/any_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use args::settings::ArgSettings;
pub trait AnyArg<'n, 'e> {
fn name(&self) -> &'n str;
fn overrides(&self) -> Option<&[&'e str]>;
fn aliases(&self) -> Option<Vec<&'e str>>;
fn requires(&self) -> Option<&[&'e str]>;
fn blacklist(&self) -> Option<&[&'e str]>;
fn required_unless(&self) -> Option<&[&'e str]>;
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ impl<'n, 'e> AnyArg<'n, 'e> for FlagBuilder<'n, 'e> {
fn longest_filter(&self) -> bool {
self.long.is_some()
}
fn aliases(&self) -> Option<Vec<&'e str>> {
None
}
}

impl<'n, 'e> DispOrder for FlagBuilder<'n, 'e> {
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 @@ -248,6 +248,9 @@ impl<'n, 'e> AnyArg<'n, 'e> for OptBuilder<'n, 'e> {
fn longest_filter(&self) -> bool {
true
}
fn aliases(&self) -> Option<Vec<&'e str>> {
None
}
}

impl<'n, 'e> DispOrder for OptBuilder<'n, 'e> {
Expand Down
3 changes: 3 additions & 0 deletions src/args/arg_builder/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ impl<'n, 'e> AnyArg<'n, 'e> for PosBuilder<'n, 'e> {
fn longest_filter(&self) -> bool {
true
}
fn aliases(&self) -> Option<Vec<&'e str>> {
None
}
}

impl<'n, 'e> DispOrder for PosBuilder<'n, 'e> {
Expand Down

0 comments on commit ca511de

Please sign in to comment.