diff --git a/src/build/command.rs b/src/build/command.rs index 7b0423874be..c228487f8e5 100644 --- a/src/build/command.rs +++ b/src/build/command.rs @@ -3640,6 +3640,11 @@ impl<'help> App<'help> { pub fn is_multicall_set(&self) -> bool { self.is_set(AppSettings::Multicall) } + + #[cfg(not(feature = "unstable-multicall"))] + fn is_multicall_set(&self) -> bool { + false + } } /// Deprecated @@ -4151,6 +4156,14 @@ impl<'help> App<'help> { } } + let self_bin_name = + if cfg!(feature = "unstable-multicall") && self.is_multicall_set() { + self.bin_name.as_deref().unwrap_or("") + } else { + self.bin_name.as_deref().unwrap_or(&self.name) + } + .to_owned(); + for mut sc in &mut self.subcommands { debug!("Command::_build_bin_names:iter: bin_name set..."); @@ -4172,12 +4185,7 @@ impl<'help> App<'help> { sc_names = format!("{{{}}}", sc_names); } - let usage_name = format!( - "{}{}{}", - self.bin_name.as_ref().unwrap_or(&self.name), - mid_string, - sc_names - ); + let usage_name = format!("{}{}{}", self_bin_name, mid_string, sc_names); debug!( "Command::_build_bin_names:iter: Setting usage_name of {} to {:?}", sc.name, usage_name @@ -4192,8 +4200,9 @@ impl<'help> App<'help> { if sc.bin_name.is_none() { let bin_name = format!( - "{} {}", - self.bin_name.as_ref().unwrap_or(&self.name), + "{}{}{}", + self_bin_name, + if !self_bin_name.is_empty() { " " } else { "" }, &*sc.name ); debug!( diff --git a/tests/builder/subcommands.rs b/tests/builder/subcommands.rs index afbcc4d23d7..836102eeb2d 100644 --- a/tests/builder/subcommands.rs +++ b/tests/builder/subcommands.rs @@ -616,3 +616,81 @@ fn cant_have_args_with_multicall() { .arg(Arg::new("oh-no")); cmd.build(); } + +#[cfg(feature = "unstable-multicall")] +#[test] +fn multicall_help_flag() { + static EXPECTED: &str = "\ +foo-bar 1.0.0 + +USAGE: + foo bar [value] + +ARGS: + + +OPTIONS: + -h, --help Print help information + -V, --version Print version information +"; + let cmd = Command::new("repl") + .version("1.0.0") + .propagate_version(true) + .multicall(true) + .subcommand(Command::new("foo").subcommand(Command::new("bar").arg(Arg::new("value")))); + utils::assert_output(cmd, "foo bar --help", EXPECTED, false); +} + +#[cfg(feature = "unstable-multicall")] +#[test] +fn multicall_help_subcommand() { + static EXPECTED: &str = "\ +foo-bar 1.0.0 + +USAGE: + foo bar [value] + +ARGS: + + +OPTIONS: + -h, --help Print help information + -V, --version Print version information +"; + let cmd = Command::new("repl") + .version("1.0.0") + .propagate_version(true) + .multicall(true) + .subcommand(Command::new("foo").subcommand(Command::new("bar").arg(Arg::new("value")))); + utils::assert_output(cmd, "help foo bar", EXPECTED, false); +} + +#[cfg(feature = "unstable-multicall")] +#[test] +fn multicall_render_help() { + static EXPECTED: &str = "\ +foo-bar 1.0.0 + +USAGE: + foo bar [value] + +ARGS: + + +OPTIONS: + -h, --help Print help information + -V, --version Print version information +"; + let mut cmd = Command::new("repl") + .version("1.0.0") + .propagate_version(true) + .multicall(true) + .subcommand(Command::new("foo").subcommand(Command::new("bar").arg(Arg::new("value")))); + cmd.build(); + let subcmd = cmd.find_subcommand_mut("foo").unwrap(); + let subcmd = subcmd.find_subcommand_mut("bar").unwrap(); + + let mut buf = Vec::new(); + subcmd.write_help(&mut buf).unwrap(); + utils::assert_eq(EXPECTED, String::from_utf8(buf).unwrap()); +}