Skip to content

Commit 7b624ca

Browse files
authored
Merge pull request #5356 from epage/escape
fix(error): Include -- in more cases
2 parents b48c90f + 446328a commit 7b624ca

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

clap_builder/src/parser/parser.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1570,10 +1570,16 @@ impl<'cmd> Parser<'cmd> {
15701570
.collect();
15711571

15721572
// `did_you_mean` is a lot more likely and should cause us to skip the `--` suggestion
1573+
// with the one exception being that the CLI is trying to capture arguments
15731574
//
15741575
// In theory, this is only called for `--long`s, so we don't need to check
1575-
let suggested_trailing_arg =
1576-
did_you_mean.is_none() && !trailing_values && self.cmd.has_positionals();
1576+
let suggested_trailing_arg = (did_you_mean.is_none()
1577+
|| self
1578+
.cmd
1579+
.get_positionals()
1580+
.any(|arg| arg.is_last_set() || arg.is_trailing_var_arg_set()))
1581+
&& !trailing_values
1582+
&& self.cmd.has_positionals();
15771583
ClapError::unknown_argument(
15781584
self.cmd,
15791585
format!("--{arg}"),

tests/builder/error.rs

+24
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ For more information, try '--help'.
150150
assert_error(err, expected_kind, MESSAGE, true);
151151
}
152152

153+
#[test]
154+
#[cfg(feature = "error-context")]
155+
fn suggest_trailing_last() {
156+
let cmd = Command::new("cargo")
157+
.arg(arg!([TESTNAME]).last(true))
158+
.arg(arg!(--"ignore-rust-version"));
159+
160+
let res = cmd.try_get_matches_from(["cargo", "--ignored"]);
161+
assert!(res.is_err());
162+
let err = res.unwrap_err();
163+
let expected_kind = ErrorKind::UnknownArgument;
164+
static MESSAGE: &str = "\
165+
error: unexpected argument '--ignored' found
166+
167+
tip: a similar argument exists: '--ignore-rust-version'
168+
tip: to pass '--ignored' as a value, use '-- --ignored'
169+
170+
Usage: cargo --ignore-rust-version [-- <TESTNAME>]
171+
172+
For more information, try '--help'.
173+
";
174+
assert_error(err, expected_kind, MESSAGE, true);
175+
}
176+
153177
#[test]
154178
#[cfg(feature = "error-context")]
155179
fn trailing_already_in_use() {

0 commit comments

Comments
 (0)