Skip to content

Commit

Permalink
fix(help): write_help usage includes required arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 30, 2022
1 parent 515a37d commit 37ebb71
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/build/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
29 changes: 29 additions & 0 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TARGET> <ARGS> 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());
}

0 comments on commit 37ebb71

Please sign in to comment.