mirror of
https://github.com/clap-rs/clap
synced 2025-01-18 23:53:54 +00:00
imp(Aliases): improves readability of asliases in help messages
Aliases are now displayed after the help text inside a `[aliases: als1, als2, als3]` style box. Closes #526 Closes #529
This commit is contained in:
parent
50ca205e3b
commit
ca511de71f
6 changed files with 33 additions and 15 deletions
|
@ -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() {
|
||||
|
@ -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());
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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]>;
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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> {
|
||||
|
|
Loading…
Reference in a new issue