fix(error): Include -- in more cases

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.
This commit is contained in:
Ed Page 2024-02-16 06:54:57 -06:00
parent 7de6df8782
commit 446328a8d3
2 changed files with 9 additions and 2 deletions

View file

@ -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}"),

View file

@ -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>]