Skip to content

Commit

Permalink
fix(multicall): Consistently skip multicall bin in help
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 2, 2022
1 parent 8cd59fa commit af3b789
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/build/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...");

Expand All @@ -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
Expand All @@ -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!(
Expand Down
78 changes: 78 additions & 0 deletions tests/builder/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<value>
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:
<value>
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:
<value>
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());
}

0 comments on commit af3b789

Please sign in to comment.