From 13e377090c70d8cf374b4ea36b82b0689bd58f08 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 16 Aug 2023 14:54:45 -0500 Subject: [PATCH] refactor(error): Give caller control over suggestion --- clap_builder/src/error/mod.rs | 4 ++-- clap_builder/src/parser/parser.rs | 1 + tests/builder/error.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/clap_builder/src/error/mod.rs b/clap_builder/src/error/mod.rs index 772058a5fe9..af90e2756f1 100644 --- a/clap_builder/src/error/mod.rs +++ b/clap_builder/src/error/mod.rs @@ -718,7 +718,7 @@ impl Error { let mut styled_suggestion = StyledStr::new(); let _ = write!( styled_suggestion, - "'{}{sub} --{flag}{}' exists", + "'{}{sub} {flag}{}' exists", valid.render(), valid.render_reset() ); @@ -727,7 +727,7 @@ impl Error { Some((flag, None)) => { err = err.insert_context_unchecked( ContextKind::SuggestedArg, - ContextValue::String(format!("--{flag}")), + ContextValue::String(flag), ); } None => {} diff --git a/clap_builder/src/parser/parser.rs b/clap_builder/src/parser/parser.rs index d6d8e8da436..81eddc9fd61 100644 --- a/clap_builder/src/parser/parser.rs +++ b/clap_builder/src/parser/parser.rs @@ -1521,6 +1521,7 @@ impl<'cmd> Parser<'cmd> { self.start_custom_arg(matcher, arg, ValueSource::CommandLine); } } + let did_you_mean = did_you_mean.map(|(arg, cmd)| (format!("--{arg}"), cmd)); let required = self.cmd.required_graph(); let used: Vec = matcher diff --git a/tests/builder/error.rs b/tests/builder/error.rs index a5972120c17..73f039ba908 100644 --- a/tests/builder/error.rs +++ b/tests/builder/error.rs @@ -229,7 +229,7 @@ fn unknown_argument_option() { static MESSAGE: &str = "\ error: unexpected argument '--cwd ' found - tip: a similar argument exists: '---C' + tip: a similar argument exists: '-C' Usage: test [OPTIONS] @@ -259,7 +259,7 @@ fn unknown_argument_flag() { static MESSAGE: &str = "\ error: unexpected argument '--ignored' found - tip: a similar argument exists: '---- --ignored' + tip: a similar argument exists: '-- --ignored' Usage: test [OPTIONS]