Skip to content

Commit

Permalink
refactor: improve invalid subcommand handling (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jun 8, 2024
1 parent da62d61 commit aa196b1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ impl<'a, 'b, T: Runtime> Matcher<'a, 'b, T> {
if self.positional_args.is_empty() && last_args.is_empty() {
return Some(MatchError::DisplayHelp);
} else {
return Some(MatchError::InvalidSubcommand(None));
return Some(MatchError::InvalidSubcommand(
self.positional_args.first().map(|v| v.to_string()),
));
}
} else if last_cmd.positional_params.is_empty() && !self.positional_args.is_empty() {
return Some(MatchError::InvalidSubcommand(
Expand Down Expand Up @@ -982,13 +984,13 @@ impl<'a, 'b, T: Runtime> Matcher<'a, 'b, T> {
let cmd = self.last_cmd();
cmd.render_version()
}
MatchError::InvalidSubcommand(name) => {
MatchError::InvalidSubcommand(arg) => {
exit = 1;
let cmd = self.last_cmd();
let cmd_str = cmd.cmd_paths().join("-");
let names = cmd.list_subcommand_names().join(", ");
let details = match name {
Some(name) => format!("but '{name}' is not one of them"),
let details = match arg {
Some(arg) => format!("but '{arg}' is not one of them"),
None => "but one was not provided".to_string(),
};
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ prog foo

# OUTPUT
command cat >&2 <<-'EOF'
error: `prog` requires a subcommand but one was not provided
error: `prog` requires a subcommand but 'foo' is not one of them
[subcommands: cmda, cmdb]
EOF
exit 1

# BUILD_OUTPUT
error: `prog` requires a subcommand but one was not provided
[subcommands: cmda, cmdb]


0 comments on commit aa196b1

Please sign in to comment.