Skip to content

Commit 2671ca7

Browse files
committed
imp(Suggestions): suggests to use flag after subcommand when applicable
If an invalid flag is found and this flag is a valid flag for a sub-command, suggest using it after the subcommand fix #927
1 parent 49f9dc1 commit 2671ca7

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

src/app/parser.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1662,11 +1662,10 @@ impl<'a, 'b> Parser<'a, 'b>
16621662
}
16631663

16641664
fn did_you_mean_error(&self, arg: &str, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
1665-
// Didn't match a flag or option...maybe it was a typo and close to one
1665+
1666+
// Didn't match a flag or option
16661667
let suffix =
1667-
suggestions::did_you_mean_suffix(arg,
1668-
longs!(self),
1669-
suggestions::DidYouMeanMessageStyle::LongFlag);
1668+
suggestions::did_you_mean_arg_suffix(arg, longs!(self), &self.subcommands);
16701669

16711670
// Add the arg to the matches to build a proper usage string
16721671
if let Some(name) = suffix.1 {

src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ impl Error {
483483
when: color,
484484
});
485485
let suffix =
486-
suggestions::did_you_mean_suffix(bad_val.as_ref(),
487-
good_vals.iter(),
488-
suggestions::DidYouMeanMessageStyle::EnumValue);
486+
suggestions::did_you_mean_value_suffix(
487+
bad_val.as_ref(),
488+
good_vals.iter());
489489

490490
let mut sorted = vec![];
491491
for v in good_vals {

src/suggestions.rs

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use app::App;
12
// Third Party
23
#[cfg(feature = "suggestions")]
34
use strsim;
@@ -40,40 +41,46 @@ pub fn did_you_mean<'a, T: ?Sized, I>(_: &str, _: I) -> Option<&'a str>
4041

4142
/// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
4243
#[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])
4645
-> (String, Option<&'z str>)
4746
where T: AsRef<str> + 'z,
4847
I: IntoIterator<Item = &'z T>
4948
{
50-
match did_you_mean(arg, values) {
49+
match did_you_mean(arg, longs) {
5150
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));
5665
}
57-
DidYouMeanMessageStyle::EnumValue => suffix.push('\''),
58-
}
59-
suffix.push_str(&Format::Good(candidate).to_string()[..]);
60-
if let DidYouMeanMessageStyle::EnumValue = style {
61-
suffix.push('\'');
6266
}
63-
suffix.push_str("?");
64-
(suffix, Some(candidate))
6567
}
66-
None => (String::new(), None),
6768
}
69+
return (String::new(), None)
6870
}
6971

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+
}
7784
}
7885

7986
#[cfg(all(test, features = "suggestions"))]

0 commit comments

Comments
 (0)