clap/
suggestions.rs
1use crate::{app::App, fmt::Format};
3
4#[cfg(feature = "suggestions")]
9#[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_lifetimes))]
10pub fn did_you_mean<'a, T: ?Sized, I>(v: &str, possible_values: I) -> Option<&'a str>
11where
12 T: AsRef<str> + 'a,
13 I: IntoIterator<Item = &'a T>,
14{
15 let mut candidate: Option<(f64, &str)> = None;
16 for pv in possible_values {
17 let confidence = strsim::jaro_winkler(v, pv.as_ref());
18 if confidence > 0.8 && (candidate.is_none() || (candidate.as_ref().unwrap().0 < confidence))
19 {
20 candidate = Some((confidence, pv.as_ref()));
21 }
22 }
23 match candidate {
24 None => None,
25 Some((_, candidate)) => Some(candidate),
26 }
27}
28
29#[cfg(not(feature = "suggestions"))]
30pub fn did_you_mean<'a, T: ?Sized, I>(_: &str, _: I) -> Option<&'a str>
31where
32 T: AsRef<str> + 'a,
33 I: IntoIterator<Item = &'a T>,
34{
35 None
36}
37
38pub fn did_you_mean_flag_suffix<'z, T, I>(
40 arg: &str,
41 args_rest: &'z [&str],
42 longs: I,
43 subcommands: &'z [App],
44) -> (String, Option<&'z str>)
45where
46 T: AsRef<str> + 'z,
47 I: IntoIterator<Item = &'z T>,
48{
49 if let Some(candidate) = did_you_mean(arg, longs) {
50 let suffix = format!(
51 "\n\tDid you mean {}{}?",
52 Format::Good("--"),
53 Format::Good(candidate)
54 );
55 return (suffix, Some(candidate));
56 }
57
58 subcommands
59 .iter()
60 .filter_map(|subcommand| {
61 let opts = subcommand
62 .p
63 .flags
64 .iter()
65 .filter_map(|f| f.s.long)
66 .chain(subcommand.p.opts.iter().filter_map(|o| o.s.long));
67
68 let candidate = match did_you_mean(arg, opts) {
69 Some(candidate) => candidate,
70 None => return None,
71 };
72 let score = match args_rest.iter().position(|x| *x == subcommand.get_name()) {
73 Some(score) => score,
74 None => return None,
75 };
76
77 let suffix = format!(
78 "\n\tDid you mean to put '{}{}' after the subcommand '{}'?",
79 Format::Good("--"),
80 Format::Good(candidate),
81 Format::Good(subcommand.get_name())
82 );
83
84 Some((score, (suffix, Some(candidate))))
85 })
86 .min_by_key(|&(score, _)| score)
87 .map(|(_, suggestion)| suggestion)
88 .unwrap_or_else(|| (String::new(), None))
89}
90
91pub fn did_you_mean_value_suffix<'z, T, I>(arg: &str, values: I) -> (String, Option<&'z str>)
93where
94 T: AsRef<str> + 'z,
95 I: IntoIterator<Item = &'z T>,
96{
97 match did_you_mean(arg, values) {
98 Some(candidate) => {
99 let suffix = format!("\n\tDid you mean '{}'?", Format::Good(candidate));
100 (suffix, Some(candidate))
101 }
102 None => (String::new(), None),
103 }
104}
105
106#[cfg(all(test, features = "suggestions"))]
107mod test {
108 use super::*;
109
110 #[test]
111 fn possible_values_match() {
112 let p_vals = ["test", "possible", "values"];
113 assert_eq!(did_you_mean("tst", p_vals.iter()), Some("test"));
114 }
115
116 #[test]
117 fn possible_values_nomatch() {
118 let p_vals = ["test", "possible", "values"];
119 assert!(did_you_mean("hahaahahah", p_vals.iter()).is_none());
120 }
121
122 #[test]
123 fn suffix_long() {
124 let p_vals = ["test", "possible", "values"];
125 let suffix = "\n\tDid you mean \'--test\'?";
126 assert_eq!(
127 did_you_mean_flag_suffix("tst", p_vals.iter(), []),
128 (suffix, Some("test"))
129 );
130 }
131
132 #[test]
133 fn suffix_enum() {
134 let p_vals = ["test", "possible", "values"];
135 let suffix = "\n\tDid you mean \'test\'?";
136 assert_eq!(
137 did_you_mean_value_suffix("tst", p_vals.iter()),
138 (suffix, Some("test"))
139 );
140 }
141}