diff --git a/clap_builder/src/parser/parser.rs b/clap_builder/src/parser/parser.rs index c1642001619..eade3e613dc 100644 --- a/clap_builder/src/parser/parser.rs +++ b/clap_builder/src/parser/parser.rs @@ -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}"), diff --git a/tests/builder/error.rs b/tests/builder/error.rs index 833a614d265..a2ea6694c26 100644 --- a/tests/builder/error.rs +++ b/tests/builder/error.rs @@ -150,6 +150,30 @@ For more information, try '--help'. assert_error(err, expected_kind, MESSAGE, true); } +#[test] +#[cfg(feature = "error-context")] +fn suggest_trailing_last() { + let cmd = Command::new("cargo") + .arg(arg!([TESTNAME]).last(true)) + .arg(arg!(--"ignore-rust-version")); + + let res = cmd.try_get_matches_from(["cargo", "--ignored"]); + assert!(res.is_err()); + let err = res.unwrap_err(); + let expected_kind = ErrorKind::UnknownArgument; + static MESSAGE: &str = "\ +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 [-- ] + +For more information, try '--help'. +"; + assert_error(err, expected_kind, MESSAGE, true); +} + #[test] #[cfg(feature = "error-context")] fn trailing_already_in_use() {