mirror of
https://github.com/clap-rs/clap
synced 2025-01-18 23:53:54 +00:00
Merge branch 'fix-subcommand-name' of https://github.com/starkat99/clap-rs into starkat99-fix-subcommand-name
This commit is contained in:
commit
32dba379d4
4 changed files with 26 additions and 9 deletions
|
@ -712,6 +712,20 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
if let Some(p) = self.positionals.get(pos_counter) {
|
if let Some(p) = self.positionals.get(pos_counter) {
|
||||||
parse_positional!(self, p, arg_os, pos_counter, matcher);
|
parse_positional!(self, p, arg_os, pos_counter, matcher);
|
||||||
} else if self.settings.is_set(AppSettings::AllowExternalSubcommands) {
|
} 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();
|
let mut sc_m = ArgMatcher::new();
|
||||||
while let Some(v) = it.next() {
|
while let Some(v) = it.next() {
|
||||||
let a = v.into();
|
let a = v.into();
|
||||||
|
@ -722,11 +736,11 @@ impl<'a, 'b> Parser<'a, 'b>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sc_m.add_val_to("EXTERNAL_SUBCOMMAND", &a);
|
sc_m.add_val_to("", &a);
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.subcommand(SubCommand {
|
matcher.subcommand(SubCommand {
|
||||||
name: "EXTERNAL_SUBCOMMAND".into(),
|
name: sc_name,
|
||||||
matches: sc_m.into(),
|
matches: sc_m.into(),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -347,11 +347,12 @@ pub enum AppSettings {
|
||||||
/// "myprog", "subcmd", "--option", "value", "-fff", "--flag"
|
/// "myprog", "subcmd", "--option", "value", "-fff", "--flag"
|
||||||
/// ]);
|
/// ]);
|
||||||
///
|
///
|
||||||
/// // All trailing arguments will be stored under the subcommand's sub-matches using a value
|
/// // All trailing arguments will be stored under the subcommand's sub-matches using an empty
|
||||||
/// // of the runtime subcommand name (in this case "subcmd")
|
/// // string argument name
|
||||||
/// match m.subcommand() {
|
/// match m.subcommand() {
|
||||||
/// (external, Some(ext_m)) => {
|
/// (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"]);
|
/// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]);
|
||||||
/// },
|
/// },
|
||||||
/// _ => {},
|
/// _ => {},
|
||||||
|
|
|
@ -493,11 +493,12 @@ impl<'a> ArgMatches<'a> {
|
||||||
/// "myprog", "subcmd", "--option", "value", "-fff", "--flag"
|
/// "myprog", "subcmd", "--option", "value", "-fff", "--flag"
|
||||||
/// ]);
|
/// ]);
|
||||||
///
|
///
|
||||||
/// // All trailing arguments will be stored under the subcommand's sub-matches using a value
|
/// // All trailing arguments will be stored under the subcommand's sub-matches using an empty
|
||||||
/// // of the runtime subcommand name (in this case "subcmd")
|
/// // string argument name
|
||||||
/// match app_m.subcommand() {
|
/// match app_m.subcommand() {
|
||||||
/// (external, Some(sub_m)) => {
|
/// (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"]);
|
/// assert_eq!(ext_args, ["--option", "value", "-fff", "--flag"]);
|
||||||
/// },
|
/// },
|
||||||
/// _ => {},
|
/// _ => {},
|
||||||
|
|
|
@ -51,7 +51,8 @@ fn build_new_help(app: &App) -> String {
|
||||||
|
|
||||||
fn compare_app_str(l: &App, right: &str) -> bool {
|
fn compare_app_str(l: &App, right: &str) -> bool {
|
||||||
let left = build_new_help(&l);
|
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 {
|
if !b {
|
||||||
println!("");
|
println!("");
|
||||||
println!("--> left");
|
println!("--> left");
|
||||||
|
|
Loading…
Reference in a new issue