refactor(did-you-mean): dedup. thanks to suffix

That way, we use the prefix previously used by clap, but add our
particular 'did-you-mean' phrase as a suffix.
This commit is contained in:
Sebastian Thiel 2015-05-05 15:22:52 +02:00 committed by Kevin K
parent 52a0b8505c
commit 0dd095f975
2 changed files with 21 additions and 22 deletions

View file

@ -40,9 +40,9 @@ USAGE:
\tclaptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
For more information try --help'''
_arg_dym_usage = '''The argument --optio is unknown. Did you mean --option ?
_arg_dym_usage = '''The argument --optio isn't valid. Did you mean --option ?
USAGE:
\tclaptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
\tclaptests
For more information try --help'''
_excluded = '''The argument '--flag' cannot be used with '-F'

View file

@ -1788,26 +1788,25 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
return None;
}
match did_you_mean(arg, self.opts.values()
.filter_map(|v|
if let Some(l) = v.long {Some(l)} else {None}
)
.collect::<Vec<_>>().iter()) {
Some(candidate_flag) => {
self.report_error(format!("The argument --{} is unknown. Did you mean --{} ?",
arg,
candidate_flag),
true,
true,
None);
},
None => {
self.report_error(format!("The argument --{} isn't valid", arg),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
}
}
let suffix =
match did_you_mean(arg, self.opts.values()
.filter_map(|v|
if let Some(ref l) = v.long {
Some(l)
} else {
None
}
)) {
Some(candidate_flag) => {
format!(". Did you mean --{} ?", candidate_flag)
},
None => String::new()
};
self.report_error(format!("The argument --{} isn't valid{}", arg, suffix),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
unreachable!();
}