Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6590a85

Browse files
committed
fix(parser): Don't suggest -- as often
See #2766
1 parent 76b891d commit 6590a85

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

clap_builder/src/error/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ impl<F: ErrorFormatter> Error<F> {
446446
subcmd: String,
447447
did_you_mean: Vec<String>,
448448
name: String,
449+
suggested_trailing_arg: bool,
449450
usage: Option<StyledStr>,
450451
) -> Self {
451452
use std::fmt::Write as _;
@@ -456,15 +457,19 @@ impl<F: ErrorFormatter> Error<F> {
456457

457458
#[cfg(feature = "error-context")]
458459
{
459-
let mut styled_suggestion = StyledStr::new();
460-
let _ = write!(
461-
styled_suggestion,
462-
"to pass '{}{subcmd}{}' as a value, use '{}{name} -- {subcmd}{}'",
463-
invalid.render(),
464-
invalid.render_reset(),
465-
valid.render(),
466-
valid.render_reset()
467-
);
460+
let mut suggestions = vec![];
461+
if suggested_trailing_arg {
462+
let mut styled_suggestion = StyledStr::new();
463+
let _ = write!(
464+
styled_suggestion,
465+
"to pass '{}{subcmd}{}' as a value, use '{}{name} -- {subcmd}{}'",
466+
invalid.render(),
467+
invalid.render_reset(),
468+
valid.render(),
469+
valid.render_reset()
470+
);
471+
suggestions.push(styled_suggestion);
472+
}
468473

469474
err = err.extend_context_unchecked([
470475
(ContextKind::InvalidSubcommand, ContextValue::String(subcmd)),
@@ -474,7 +479,7 @@ impl<F: ErrorFormatter> Error<F> {
474479
),
475480
(
476481
ContextKind::Suggested,
477-
ContextValue::StyledStrs(vec![styled_suggestion]),
482+
ContextValue::StyledStrs(suggestions),
478483
),
479484
]);
480485
if let Some(usage) = usage {

clap_builder/src/parser/parser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ impl<'cmd> Parser<'cmd> {
474474
}
475475
}
476476

477+
let suggested_trailing_arg = !trailing_values
478+
&& self.cmd.has_positionals()
479+
&& (arg_os.is_long() || arg_os.is_short());
480+
477481
if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
478482
let candidates = suggestions::did_you_mean(
479483
&arg_os.display().to_string(),
@@ -489,6 +493,7 @@ impl<'cmd> Parser<'cmd> {
489493
.get_bin_name()
490494
.unwrap_or_else(|| self.cmd.get_name())
491495
.to_owned(),
496+
suggested_trailing_arg,
492497
Usage::new(self.cmd).create_usage_with_title(&[]),
493498
);
494499
}
@@ -505,9 +510,6 @@ impl<'cmd> Parser<'cmd> {
505510
}
506511
}
507512

508-
let suggested_trailing_arg = !trailing_values
509-
&& self.cmd.has_positionals()
510-
&& (arg_os.is_long() || arg_os.is_short());
511513
ClapError::unknown_argument(
512514
self.cmd,
513515
arg_os.display().to_string(),

tests/builder/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ fn cant_use_trailing_subcommand() {
202202
error: unrecognized subcommand 'baz'
203203
204204
tip: a similar subcommand exists: 'bar'
205-
tip: to pass 'baz' as a value, use 'test -- baz'
206205
207206
Usage: test [COMMAND]
208207

tests/builder/subcommands.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ fn subcmd_did_you_mean_output() {
103103
error: unrecognized subcommand 'subcm'
104104
105105
tip: a similar subcommand exists: 'subcmd'
106-
tip: to pass 'subcm' as a value, use 'dym -- subcm'
107106
108107
Usage: dym [COMMAND]
109108
@@ -123,7 +122,6 @@ fn subcmd_did_you_mean_output_ambiguous() {
123122
error: unrecognized subcommand 'te'
124123
125124
tip: some similar subcommands exist: 'test', 'temp'
126-
tip: to pass 'te' as a value, use 'dym -- te'
127125
128126
Usage: dym [COMMAND]
129127
@@ -504,7 +502,6 @@ For more information, try 'help'.
504502
error: unrecognized subcommand 'baz'
505503
506504
tip: a similar subcommand exists: 'bar'
507-
tip: to pass 'baz' as a value, use ' -- baz'
508505
509506
Usage: <COMMAND>
510507

0 commit comments

Comments
 (0)