From 994474146e9fb8b701af773a52da71553d74d4b7 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Wed, 6 May 2015 12:13:32 -0400 Subject: [PATCH] im(suggestions): adds suggested arguments to usage strings --- src/app.rs | 63 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/app.rs b/src/app.rs index 058007bbaa9..bea0181cd34 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1233,7 +1233,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ /// Returns a suffix that can be empty, or is the standard 'did you mean phrase fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle) - -> String + -> (String, Option<&'z str>) where T: AsRef + 'z, I: IntoIterator { match did_you_mean(arg, values) { @@ -1248,9 +1248,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ suffix.push('\''); } suffix.push_str(" ?"); - suffix + (suffix, Some(candidate)) }, - None => String::new(), + None => (String::new(), None), } } @@ -1267,7 +1267,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ .fold(String::new(), |acc, name| { acc + &format!(" {}",name)[..] })), - suffix), + suffix.0), true, true, Some(matches.args.keys().map(|k| *k).collect())); @@ -1836,27 +1836,40 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ return None; } - let mut suffix = App::did_you_mean_suffix(arg, self.opts.values() - .filter_map(|v| - if let Some(ref l) = v.long { - Some(l) - } else { - None - } - ), DidYouMeanMessageStyle::LongFlag); - - // If it didn't find a good match for opts, try flags - if suffix.is_empty() { - suffix = App::did_you_mean_suffix(arg, self.flags.values() - .filter_map(|v| - if let Some(ref l) = v.long { - Some(l) - } else { - None - } - ), DidYouMeanMessageStyle::LongFlag); - } - self.report_error(format!("The argument --{} isn't valid{}", arg, suffix), + let suffix = App::did_you_mean_suffix(arg, + self.long_list.iter(), + DidYouMeanMessageStyle::LongFlag); + if let Some(name) = suffix.1 { + if let Some(ref opt) = self.opts.values() + .filter_map(|ref o| { + if o.long.is_some() && o.long.unwrap() == name { + Some(o.name) + } else { + None + } + }) + .next() { + matches.args.insert(opt, MatchedArg { + occurrences: 0, + values: None + }); + } else if let Some(ref flg) = self.flags.values() + .filter_map(|ref f| { + if f.long.is_some() && f.long.unwrap() == name { + Some(f.name) + } else { + None + } + }) + .next() { + matches.args.insert(flg, MatchedArg { + occurrences: 0, + values: None + }); + } + } + + self.report_error(format!("The argument --{} isn't valid{}", arg, suffix.0), true, true, Some(matches.args.keys().map(|k| *k).collect()));