Skip to content

Commit

Permalink
fix(error): Include -- in more cases
Browse files Browse the repository at this point in the history
Inspired by rust-lang/cargo#12494.
Part of this is that our "did you mean" does prefix checks so it can be
overly aggressive in providing suggestions.
To avoid providing needless suggestions I limited this change to `last`
/ `trailing_var_arg` as those convey that `--` is more likely a valid
suggestion.
  • Loading branch information
epage committed Feb 16, 2024
1 parent 7de6df8 commit 446328a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions clap_builder/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,16 @@ impl<'cmd> Parser<'cmd> {
.collect();

// `did_you_mean` is a lot more likely and should cause us to skip the `--` suggestion
// with the one exception being that the CLI is trying to capture arguments
//
// In theory, this is only called for `--long`s, so we don't need to check
let suggested_trailing_arg =
did_you_mean.is_none() && !trailing_values && self.cmd.has_positionals();
let suggested_trailing_arg = (did_you_mean.is_none()
|| self
.cmd
.get_positionals()
.any(|arg| arg.is_last_set() || arg.is_trailing_var_arg_set()))
&& !trailing_values
&& self.cmd.has_positionals();
ClapError::unknown_argument(
self.cmd,
format!("--{arg}"),
Expand Down
1 change: 1 addition & 0 deletions tests/builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ fn suggest_trailing_last() {
error: unexpected argument '--ignored' found
tip: a similar argument exists: '--ignore-rust-version'
tip: to pass '--ignored' as a value, use '-- --ignored'
Usage: cargo --ignore-rust-version [-- <TESTNAME>]
Expand Down

0 comments on commit 446328a

Please sign in to comment.