Skip to content

Commit

Permalink
fix(parser): Be less confusing with args/subcommand conflicts
Browse files Browse the repository at this point in the history
The new error message still isn't great but its better than the old one.

Reported at https://hachyderm.io/@eminence/109548978776785113
  • Loading branch information
epage committed Dec 21, 2022
1 parent 2a374db commit 453ac0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
52 changes: 27 additions & 25 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,33 +502,35 @@ impl<'cmd> Parser<'cmd> {
}
}

let candidates = suggestions::did_you_mean(
&arg_os.display().to_string(),
self.cmd.all_subcommand_names(),
);
// If the argument looks like a subcommand.
if !candidates.is_empty() {
return ClapError::invalid_subcommand(
self.cmd,
arg_os.display().to_string(),
candidates,
self.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
.to_owned(),
Usage::new(self.cmd).create_usage_with_title(&[]),
if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
let candidates = suggestions::did_you_mean(
&arg_os.display().to_string(),
self.cmd.all_subcommand_names(),
);
}
// If the argument looks like a subcommand.
if !candidates.is_empty() {
return ClapError::invalid_subcommand(
self.cmd,
arg_os.display().to_string(),
candidates,
self.cmd
.get_bin_name()
.unwrap_or_else(|| self.cmd.get_name())
.to_owned(),
Usage::new(self.cmd).create_usage_with_title(&[]),
);
}

// If the argument must be a subcommand.
if self.cmd.has_subcommands()
&& (!self.cmd.has_positionals() || self.cmd.is_infer_subcommands_set())
{
return ClapError::unrecognized_subcommand(
self.cmd,
arg_os.display().to_string(),
Usage::new(self.cmd).create_usage_with_title(&[]),
);
// If the argument must be a subcommand.
if self.cmd.has_subcommands()
&& (!self.cmd.has_positionals() || self.cmd.is_infer_subcommands_set())
{
return ClapError::unrecognized_subcommand(
self.cmd,
arg_os.display().to_string(),
Usage::new(self.cmd).create_usage_with_title(&[]),
);
}
}

let suggested_trailing_arg = !trailing_values
Expand Down
6 changes: 1 addition & 5 deletions tests/builder/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,7 @@ fn args_negate_subcommands_two_levels() {
#[cfg(feature = "error-context")]
fn subcommand_conflict_error_message() {
static CONFLICT_ERR: &str = "\
error: The subcommand 'sub1' wasn't recognized
Did you mean 'sub1'?
If you believe you received this message in error, try re-running with 'test -- sub1'
error: Found argument 'sub1' which wasn't expected, or isn't valid in this context
Usage: test [OPTIONS]
test <COMMAND>
Expand Down

0 comments on commit 453ac0b

Please sign in to comment.