diff --git a/src/build/command.rs b/src/build/command.rs index 02efb6d8f74..d9697af5f12 100644 --- a/src/build/command.rs +++ b/src/build/command.rs @@ -4128,14 +4128,59 @@ impl<'help> App<'help> { debug!("Command::_build_bin_names"); if !self.is_set(AppSettings::BinNameBuilt) { + let mut mid_string = String::from(" "); + if !self.is_subcommand_negates_reqs_set() { + let reqs = Usage::new(self).get_required_usage_from(&[], None, true); // maybe Some(m) + + for s in &reqs { + mid_string.push_str(s); + mid_string.push(' '); + } + } + for mut sc in &mut self.subcommands { debug!("Command::_build_bin_names:iter: bin_name set..."); + if sc.usage_name.is_none() { + use std::fmt::Write; + // Display subcommand name, short and long in usage + let mut sc_names = sc.name.clone(); + let mut flag_subcmd = false; + if let Some(l) = sc.long_flag { + write!(sc_names, "|--{}", l).unwrap(); + flag_subcmd = true; + } + if let Some(s) = sc.short_flag { + write!(sc_names, "|-{}", s).unwrap(); + flag_subcmd = true; + } + + if flag_subcmd { + sc_names = format!("{{{}}}", sc_names); + } + + let usage_name = format!( + "{}{}{}", + self.bin_name.as_ref().unwrap_or(&self.name), + mid_string, + sc_names + ); + debug!( + "Command::_build_bin_names:iter: Setting usage_name of {} to {:?}", + sc.name, usage_name + ); + sc.usage_name = Some(usage_name); + } else { + debug!( + "Command::_build_bin_names::iter: Using existing usage_name of {} ({:?})", + sc.name, sc.usage_name + ); + } + if sc.bin_name.is_none() { let bin_name = format!( - "{}{}{}", + "{} {}", self.bin_name.as_ref().unwrap_or(&self.name), - if self.bin_name.is_some() { " " } else { "" }, &*sc.name ); debug!( diff --git a/tests/builder/help.rs b/tests/builder/help.rs index 0e255e9aff0..bc2983875eb 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -2673,3 +2673,32 @@ OPTIONS: .subcommand(Command::new("test").about("some")); utils::assert_output(cmd, "parent help test", EXPECTED, false); } + +#[test] +fn parent_cmd_req_in_usage_with_render_help() { + static EXPECTED: &str = "parent-test +some + +USAGE: + parent test + +OPTIONS: + -h, --help Print help information +"; + let mut cmd = Command::new("parent") + .version("0.1") + .arg(Arg::new("TARGET").required(true).help("some")) + .arg( + Arg::new("ARGS") + .takes_value(true) + .required(true) + .help("some"), + ) + .subcommand(Command::new("test").about("some")); + cmd.build(); + let subcmd = cmd.find_subcommand_mut("test").unwrap(); + + let mut buf = Vec::new(); + subcmd.write_help(&mut buf).unwrap(); + utils::assert_eq(EXPECTED, String::from_utf8(buf).unwrap()); +}