diff --git a/clap_builder/src/builder/value_parser.rs b/clap_builder/src/builder/value_parser.rs index 48711aa0752..ab06ae5a432 100644 --- a/clap_builder/src/builder/value_parser.rs +++ b/clap_builder/src/builder/value_parser.rs @@ -2,6 +2,7 @@ use std::convert::TryInto; use std::ops::RangeBounds; use crate::builder::Str; +use crate::builder::StyledStr; use crate::util::AnyValue; use crate::util::AnyValueId; @@ -2104,7 +2105,7 @@ where /// Arg::new("current-dir-unknown") /// .long("cwd") /// .aliases(["current-dir", "directory", "working-directory", "root"]) -/// .value_parser(clap::builder::UnknownArgumentValueParser::suggest("-C")) +/// .value_parser(clap::builder::UnknownArgumentValueParser::suggest_arg("-C")) /// .hide(true), /// ]); /// @@ -2122,13 +2123,31 @@ where /// ``` #[derive(Clone, Debug)] pub struct UnknownArgumentValueParser { - arg: Str, + arg: Option, + suggestions: Vec, } impl UnknownArgumentValueParser { /// Suggest an alternative argument - pub fn suggest(arg: impl Into) -> Self { - Self { arg: arg.into() } + pub fn suggest_arg(arg: impl Into) -> Self { + Self { + arg: Some(arg.into()), + suggestions: Default::default(), + } + } + + /// Provide a general suggestion + pub fn suggest(text: impl Into) -> Self { + Self { + arg: Default::default(), + suggestions: vec![text.into()], + } + } + + /// Extend the suggestions + pub fn and_suggest(mut self, text: impl Into) -> Self { + self.suggestions.push(text.into()); + self } } @@ -2145,13 +2164,26 @@ impl TypedValueParser for UnknownArgumentValueParser { Some(arg) => arg.to_string(), None => "..".to_owned(), }; - Err(crate::Error::unknown_argument( + let err = crate::Error::unknown_argument( cmd, arg, - Some((self.arg.as_str().to_owned(), None)), + self.arg.as_ref().map(|s| (s.as_str().to_owned(), None)), false, crate::output::Usage::new(cmd).create_usage_with_title(&[]), - )) + ); + #[cfg(feature = "error-context")] + let err = { + debug_assert_eq!( + err.get(crate::error::ContextKind::Suggested), + None, + "Assuming `Error::unknown_argument` doesn't apply any `Suggested` so we can without caution" + ); + err.insert_context_unchecked( + crate::error::ContextKind::Suggested, + crate::error::ContextValue::StyledStrs(self.suggestions.clone()), + ) + }; + Err(err) } } 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..bfdf623cb5b 100644 --- a/tests/builder/error.rs +++ b/tests/builder/error.rs @@ -219,7 +219,10 @@ fn unknown_argument_option() { Arg::new("current-dir-unknown") .long("cwd") .aliases(["current-dir", "directory", "working-directory", "root"]) - .value_parser(clap::builder::UnknownArgumentValueParser::suggest("-C")) + .value_parser( + clap::builder::UnknownArgumentValueParser::suggest_arg("-C") + .and_suggest("not much else to say"), + ) .hide(true), ]); let res = cmd.try_get_matches_from(["test", "--cwd", ".."]); @@ -229,7 +232,8 @@ fn unknown_argument_option() { static MESSAGE: &str = "\ error: unexpected argument '--cwd ' found - tip: a similar argument exists: '---C' + tip: a similar argument exists: '-C' + tip: not much else to say Usage: test [OPTIONS] @@ -247,9 +251,10 @@ fn unknown_argument_flag() { Arg::new("libtest-ignore") .long("ignored") .action(ArgAction::SetTrue) - .value_parser(clap::builder::UnknownArgumentValueParser::suggest( - "-- --ignored", - )) + .value_parser( + clap::builder::UnknownArgumentValueParser::suggest_arg("-- --ignored") + .and_suggest("not much else to say"), + ) .hide(true), ]); let res = cmd.try_get_matches_from(["test", "--ignored"]); @@ -259,7 +264,8 @@ fn unknown_argument_flag() { static MESSAGE: &str = "\ error: unexpected argument '--ignored' found - tip: a similar argument exists: '---- --ignored' + tip: a similar argument exists: '-- --ignored' + tip: not much else to say Usage: test [OPTIONS]