Skip to content

Commit 446328a

Browse files
committed
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.
1 parent 7de6df8 commit 446328a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-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

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ fn suggest_trailing_last() {
165165
error: unexpected argument '--ignored' found
166166
167167
tip: a similar argument exists: '--ignore-rust-version'
168+
tip: to pass '--ignored' as a value, use '-- --ignored'
168169
169170
Usage: cargo --ignore-rust-version [-- <TESTNAME>]
170171

0 commit comments

Comments
 (0)