Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions tests/builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [-- <TESTNAME>]

For more information, try '--help'.
";
assert_error(err, expected_kind, MESSAGE, true);
}

#[test]
#[cfg(feature = "error-context")]
fn trailing_already_in_use() {
Expand Down