Skip to content

Commit

Permalink
Merge pull request #5356 from epage/escape
Browse files Browse the repository at this point in the history
fix(error): Include -- in more cases
  • Loading branch information
epage authored Feb 16, 2024
2 parents b48c90f + 446328a commit 7b624ca
Show file tree
Hide file tree
Showing 2 changed files with 32 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
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

0 comments on commit 7b624ca

Please sign in to comment.