Skip to content

Commit

Permalink
Auto merge of #530 - kbknapp:issues-526,528,529, r=kbknapp
Browse files Browse the repository at this point in the history
Issues 526,528,529
  • Loading branch information
homu committed Jun 13, 2016
2 parents 50ca205 + e468faf commit 77270cb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 19 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
44 changes: 44 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,12 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
if let Some(v) = yaml["about"].as_str() {
a = a.about(v);
}
if let Some(v) = yaml["before_help"].as_str() {
a = a.before_help(v);
}
if let Some(v) = yaml["template"].as_str() {
a = a.template(v);
}
if let Some(v) = yaml["after_help"].as_str() {
a = a.after_help(v);
}
Expand All @@ -1157,20 +1163,46 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
if let Some(v) = yaml["version_short"].as_str() {
a = a.version_short(v);
}
if let Some(v) = yaml["setting"].as_str() {
a = a.setting(v.parse().ok().expect("unknown AppSetting found in YAML file"));
}
if let Some(v) = yaml["settings"].as_vec() {
for ys in v {
if let Some(s) = ys.as_str() {
a = a.setting(s.parse().ok().expect("unknown AppSetting found in YAML file"));
}
}
}
if let Some(v) = yaml["global_setting"].as_str() {
a = a.setting(v.parse().ok().expect("unknown AppSetting found in YAML file"));
}
if let Some(v) = yaml["global_settings"].as_vec() {
for ys in v {
if let Some(s) = ys.as_str() {
a = a.global_setting(s.parse().ok().expect("unknown AppSetting found in YAML file"));
}
}
}
if let Some(v) = yaml["alias"].as_str() {
a = a.alias(v);
}
if let Some(v) = yaml["aliases"].as_vec() {
for ys in v {
if let Some(s) = ys.as_str() {
a = a.alias(s);
}
}
}
if let Some(v) = yaml["visible_alias"].as_str() {
a = a.visible_alias(v);
}
if let Some(v) = yaml["visible_aliases"].as_vec() {
for ys in v {
if let Some(s) = ys.as_str() {
a = a.visible_alias(s);
}
}
}
if let Some(v) = yaml["args"].as_vec() {
for arg_yaml in v {
a = a.arg(Arg::from_yaml(&arg_yaml.as_hash().unwrap()));
Expand Down Expand Up @@ -1261,6 +1293,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.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ impl<'a, 'b> Arg<'a, 'b> {
"index" => a.index(v.as_i64().unwrap() as u64),
"global" => a.global(v.as_bool().unwrap()),
"multiple" => a.multiple(v.as_bool().unwrap()),
"hidden" => a.hidden(v.as_bool().unwrap()),
"next_line_help" => a.next_line_help(v.as_bool().unwrap()),
"empty_values" => a.empty_values(v.as_bool().unwrap()),
"group" => a.group(v.as_str().unwrap()),
"number_of_values" => a.number_of_values(v.as_i64().unwrap() as u64),
Expand All @@ -165,6 +167,7 @@ impl<'a, 'b> Arg<'a, 'b> {
"value_delimiter" => a.value_delimiter(v.as_str().unwrap()),
"required_unless" => a.required_unless(v.as_str().unwrap()),
"display_order" => a.display_order(v.as_i64().unwrap() as usize),
"default_value" => a.default_value(v.as_str().unwrap()),
"value_names" => {
for ys in v.as_vec().unwrap() {
if let Some(s) = ys.as_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
32 changes: 28 additions & 4 deletions tests/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ FLAGS:
-V, --version Prints version information
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
vim|vi Some help";
help Prints this message or the help of the given subcommand(s)
test Some help [aliases: dongle, done]";

static INVISIBLE_ALIAS_HELP: &'static str = "clap-test 2.6
USAGE:
clap-test [FLAGS] [SUBCOMMAND]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
test Some help";

#[test]
fn subcommand() {
Expand Down Expand Up @@ -121,9 +134,20 @@ fn alias_help() {
fn visible_aliases_help_output() {
let app = App::new("clap-test")
.version("2.6")
.subcommand(SubCommand::with_name("vim")
.subcommand(SubCommand::with_name("test")
.about("Some help")
.alias("invisible")
.visible_alias("vi"));
.visible_alias("dongle")
.visible_alias("done"));
test::check_help(app, VISIBLE_ALIAS_HELP);
}

#[test]
fn invisible_aliases_help_output() {
let app = App::new("clap-test")
.version("2.6")
.subcommand(SubCommand::with_name("test")
.about("Some help")
.alias("invisible"));
test::check_help(app, INVISIBLE_ALIAS_HELP);
}

0 comments on commit 77270cb

Please sign in to comment.