From aebc8fd8285fb600272672cf65244e5011ba77a1 Mon Sep 17 00:00:00 2001 From: Kathryn Long Date: Sat, 16 Jul 2016 16:10:26 -0500 Subject: [PATCH 1/2] tests(template): fix template asserts on windows --- tests/template_help.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/template_help.rs b/tests/template_help.rs index 420111e1..e3fda73b 100644 --- a/tests/template_help.rs +++ b/tests/template_help.rs @@ -51,7 +51,8 @@ fn build_new_help(app: &App) -> String { fn compare_app_str(l: &App, right: &str) -> bool { let left = build_new_help(&l); - let b = left.trim() == right; + // Strip out any mismatching \r character on windows that might sneak in on either side + let b = left.trim().replace("\r", "") == right.replace("\r", ""); if !b { println!(""); println!("--> left"); From 875df24316c266920a073c13bbefbf546bc1f635 Mon Sep 17 00:00:00 2001 From: Kathryn Long Date: Sat, 16 Jul 2016 16:18:46 -0500 Subject: [PATCH 2/2] fix(parser): preserve external subcommand name BREAKING CHANGE: Access external subcommand arguments using an empty argument name (i.e. `values_of("")`) instead of the previous subcommand name. --- src/app/parser.rs | 18 ++++++++++++++++-- src/app/settings.rs | 7 ++++--- src/args/arg_matches.rs | 7 ++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/app/parser.rs b/src/app/parser.rs index 34a0a722..7ec8566f 100644 --- a/src/app/parser.rs +++ b/src/app/parser.rs @@ -708,6 +708,20 @@ impl<'a, 'b> Parser<'a, 'b> if let Some(p) = self.positionals.get(pos_counter) { parse_positional!(self, p, arg_os, pos_counter, matcher); } else if self.settings.is_set(AppSettings::AllowExternalSubcommands) { + // Get external subcommand name + let sc_name = match arg_os.to_str() { + Some(s) => s.to_string(), + None => { + if !self.settings.is_set(AppSettings::StrictUtf8) { + return Err( + Error::invalid_utf8(&*self.create_current_usage(matcher), self.color()) + ); + } + arg_os.to_string_lossy().into_owned() + } + }; + + // Collect the external subcommand args let mut sc_m = ArgMatcher::new(); while let Some(v) = it.next() { let a = v.into(); @@ -718,11 +732,11 @@ impl<'a, 'b> Parser<'a, 'b> ); } } - sc_m.add_val_to("EXTERNAL_SUBCOMMAND", &a); + sc_m.add_val_to("", &a); } matcher.subcommand(SubCommand { - name: "EXTERNAL_SUBCOMMAND".into(), + name: sc_name, matches: sc_m.into(), }); } else { diff --git a/src/app/settings.rs b/src/app/settings.rs index accc5ce1..15005d6d 100644 --- a/src/app/settings.rs +++ b/src/app/settings.rs @@ -347,11 +347,12 @@ pub enum AppSettings { /// "myprog", "subcmd", "--option", "value", "-fff", "--flag" /// ]); /// - /// // All trailing arguments will be stored under the subcommand's sub-matches using a value - /// // of the runtime subcommand name (in this case "subcmd") + /// // All trailing arguments will be stored under the subcommand's sub-matches using an empty + /// // string argument name /// match m.subcommand() { /// (external, Some(ext_m)) => { - /// let ext_args: Vec<&str> = ext_m.values_of(external).unwrap().collect(); + /// let ext_args: Vec<&str> = ext_m.values_of("").unwrap().collect(); + /// assert_eq!(external, "subcmd"); /// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]); /// }, /// _ => {}, diff --git a/src/args/arg_matches.rs b/src/args/arg_matches.rs index e6afeb5f..aa06340b 100644 --- a/src/args/arg_matches.rs +++ b/src/args/arg_matches.rs @@ -493,11 +493,12 @@ impl<'a> ArgMatches<'a> { /// "myprog", "subcmd", "--option", "value", "-fff", "--flag" /// ]); /// - /// // All trailing arguments will be stored under the subcommand's sub-matches using a value - /// // of the runtime subcommand name (in this case "subcmd") + /// // All trailing arguments will be stored under the subcommand's sub-matches using an empty + /// // string argument name /// match app_m.subcommand() { /// (external, Some(sub_m)) => { - /// let ext_args: Vec<&str> = sub_m.values_of(external).unwrap().collect(); + /// let ext_args: Vec<&str> = sub_m.values_of("").unwrap().collect(); + /// assert_eq!(external, "subcmd"); /// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]); /// }, /// _ => {},