Skip to content

Commit

Permalink
im(suggestions): adds suggested arguments to usage strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed May 6, 2015
1 parent 55f7111 commit 9944741
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str> + 'z,
I: IntoIterator<Item=&'z T> {
match did_you_mean(arg, values) {
Expand All @@ -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),
}
}

Expand All @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit 9944741

Please sign in to comment.