Skip to content

Commit

Permalink
fix(help): Provide correct context for help subcmd errors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 30, 2022
1 parent 1452c1e commit ccf8634
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,10 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
sc._build_subcommand(&sc_name).unwrap()
} else {
return Err(ClapError::unrecognized_subcommand(
self.cmd,
&sc,
cmd.to_string_lossy().into_owned(),
self.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
sc.get_bin_name()
.unwrap_or_else(|| sc.get_name())
.to_owned(),
));
};
Expand Down
35 changes: 35 additions & 0 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,41 @@ fn help_subcommand() {
assert_eq!(m.unwrap_err().kind(), ErrorKind::DisplayHelp);
}

#[test]
fn help_multi_subcommand_error() {
let cmd = Command::new("ctest").subcommand(
Command::new("subcmd").subcommand(
Command::new("multi")
.about("tests subcommands")
.author("Kevin K. <kbknapp@gmail.com>")
.version("0.1")
.arg(arg!(
-f --flag "tests flags"
))
.arg(
arg!(
-o --option <scoption> "tests options"
)
.required(false)
.multiple_values(true)
.multiple_occurrences(true),
),
),
);
let err = cmd
.try_get_matches_from(["ctest", "help", "subcmd", "multi", "foo"])
.unwrap_err();

static EXPECTED: &str = "error: The subcommand 'foo' wasn't recognized
USAGE:
ctest subcmd multi <subcommands>
For more information try --help
";
utils::assert_eq(EXPECTED, err.to_string());
}

#[test]
fn req_last_arg_usage() {
let cmd = Command::new("example")
Expand Down

0 comments on commit ccf8634

Please sign in to comment.