|
| 1 | +use app::App; |
1 | 2 | // Third Party
|
2 | 3 | #[cfg(feature = "suggestions")]
|
3 | 4 | use strsim;
|
@@ -40,40 +41,46 @@ pub fn did_you_mean<'a, T: ?Sized, I>(_: &str, _: I) -> Option<&'a str>
|
40 | 41 |
|
41 | 42 | /// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
|
42 | 43 | #[cfg_attr(feature = "lints", allow(needless_lifetimes))]
|
43 |
| -pub fn did_you_mean_suffix<'z, T, I>(arg: &str, |
44 |
| - values: I, |
45 |
| - style: DidYouMeanMessageStyle) |
| 44 | +pub fn did_you_mean_flag_suffix<'z, T, I>(arg: &str, longs: I, subcommands: &'z [App]) |
46 | 45 | -> (String, Option<&'z str>)
|
47 | 46 | where T: AsRef<str> + 'z,
|
48 | 47 | I: IntoIterator<Item = &'z T>
|
49 | 48 | {
|
50 |
| - match did_you_mean(arg, values) { |
| 49 | + match did_you_mean(arg, longs) { |
51 | 50 | Some(candidate) => {
|
52 |
| - let mut suffix = "\n\tDid you mean ".to_owned(); |
53 |
| - match style { |
54 |
| - DidYouMeanMessageStyle::LongFlag => { |
55 |
| - suffix.push_str(&Format::Good("--").to_string()) |
| 51 | + let suffix = format!("\n\tDid you mean {}{}?", Format::Good("--"), Format::Good(candidate)); |
| 52 | + return (suffix, Some(candidate)) |
| 53 | + } |
| 54 | + None => { |
| 55 | + for subcommand in subcommands { |
| 56 | + let opts = subcommand.p.flags.iter().filter_map(|f| f.s.long).chain( |
| 57 | + subcommand.p.opts.iter().filter_map(|o| o.s.long)); |
| 58 | + |
| 59 | + if let Some(candidate) = did_you_mean(arg, opts) { |
| 60 | + let suffix = format!( |
| 61 | + "\n\tDid you mean to put '--{}' after the subcommand '{}'?", |
| 62 | + Format::Good(arg), |
| 63 | + Format::Good(candidate)); |
| 64 | + return (suffix, Some(candidate)); |
56 | 65 | }
|
57 |
| - DidYouMeanMessageStyle::EnumValue => suffix.push('\''), |
58 |
| - } |
59 |
| - suffix.push_str(&Format::Good(candidate).to_string()[..]); |
60 |
| - if let DidYouMeanMessageStyle::EnumValue = style { |
61 |
| - suffix.push('\''); |
62 | 66 | }
|
63 |
| - suffix.push_str("?"); |
64 |
| - (suffix, Some(candidate)) |
65 | 67 | }
|
66 |
| - None => (String::new(), None), |
67 | 68 | }
|
| 69 | + return (String::new(), None) |
68 | 70 | }
|
69 | 71 |
|
70 |
| -/// A helper to determine message formatting |
71 |
| -#[derive(Copy, Clone, Debug)] |
72 |
| -pub enum DidYouMeanMessageStyle { |
73 |
| - /// Suggested value is a long flag |
74 |
| - LongFlag, |
75 |
| - /// Suggested value is one of various possible values |
76 |
| - EnumValue, |
| 72 | +/// Returns a suffix that can be empty, or is the standard 'did you mean' phrase |
| 73 | +pub fn did_you_mean_value_suffix<'z, T, I>(arg: &str, values: I) -> (String, Option<&'z str>) |
| 74 | + where T: AsRef<str> + 'z, |
| 75 | + I: IntoIterator<Item = &'z T> |
| 76 | +{ |
| 77 | + match did_you_mean(arg, values) { |
| 78 | + Some(candidate) => { |
| 79 | + let suffix = format!("\n\tDid you mean '{}'?", Format::Good(candidate)); |
| 80 | + (suffix, Some(candidate)) |
| 81 | + } |
| 82 | + None => (String::new(), None), |
| 83 | + } |
77 | 84 | }
|
78 | 85 |
|
79 | 86 | #[cfg(all(test, features = "suggestions"))]
|
|
0 commit comments