Skip to content

Commit

Permalink
fix(multicall): Improve bad multicall binary error
Browse files Browse the repository at this point in the history
By removing all arguments, we've switched from an "unrecognized
argument" error to a "unrecognized subcommand" error.  While the wording
has room for improvement, its at least progress on #2862.
  • Loading branch information
epage committed May 2, 2022
1 parent 86b0ea6 commit 414ae57
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
1 change: 0 additions & 1 deletion examples/repl.rs
Expand Up @@ -66,7 +66,6 @@ fn cli() -> Command<'static> {
Command::new("repl")
.multicall(true)
.arg_required_else_help(true)
.disable_help_flag(true)
.subcommand_required(true)
.subcommand_value_name("APPLET")
.subcommand_help_heading("APPLETS")
Expand Down
8 changes: 8 additions & 0 deletions src/build/command.rs
Expand Up @@ -4025,6 +4025,14 @@ impl<'help> App<'help> {
// Make sure all the globally set flags apply to us as well
self.settings = self.settings | self.g_settings;

#[cfg(feature = "unstable-multicall")]
{
if self.is_set(AppSettings::Multicall) {
self.settings.insert(AppSettings::DisableHelpFlag.into());
self.settings.insert(AppSettings::DisableVersionFlag.into());
}
}

self._propagate();
self._check_help_and_version();
self._propagate_global_args();
Expand Down
57 changes: 55 additions & 2 deletions tests/builder/subcommands.rs
Expand Up @@ -517,7 +517,7 @@ fn busybox_like_multicall() {

let m = cmd.clone().try_get_matches_from(&["a.out"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
}

#[cfg(feature = "unstable-multicall")]
Expand All @@ -539,7 +539,7 @@ fn hostname_like_multicall() {

let m = cmd.clone().try_get_matches_from(&["a.out"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);

let m = cmd.try_get_matches_from_mut(&["hostname", "hostname"]);
assert!(m.is_err());
Expand All @@ -549,3 +549,56 @@ fn hostname_like_multicall() {
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnknownArgument);
}

#[cfg(feature = "unstable-multicall")]
#[test]
fn bad_multicall_command_error() {
let cmd = Command::new("repl")
.version("1.0.0")
.propagate_version(true)
.multicall(true)
.subcommand(Command::new("foo"))
.subcommand(Command::new("bar"));

let err = cmd.clone().try_get_matches_from(&["world"]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnrecognizedSubcommand);
static HELLO_EXPECTED: &str = "\
error: The subcommand 'world' wasn't recognized
USAGE:
<subcommands>
For more information try help
";
utils::assert_eq(HELLO_EXPECTED, err.to_string());

let err = cmd.clone().try_get_matches_from(&["baz"]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
static BAZ_EXPECTED: &str = "\
error: The subcommand 'baz' wasn't recognized
\tDid you mean 'bar'?
If you believe you received this message in error, try re-running with ' -- baz'
USAGE:
[SUBCOMMAND]
For more information try help
";
utils::assert_eq(BAZ_EXPECTED, err.to_string());

// Verify whatever we did to get the above to work didn't disable `--help` and `--version`.

let err = cmd
.clone()
.try_get_matches_from(&["foo", "--help"])
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::DisplayHelp);

let err = cmd
.clone()
.try_get_matches_from(&["foo", "--version"])
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
}

0 comments on commit 414ae57

Please sign in to comment.