feat(did-you-mean): for long flags (i.e. --long)

Long arguments now have a special case when saying they are unknown, as
we will match it against all known long flags and suggest the best match
to be used instead.

TODO: refactor to just write a suffix with did-you-mean information.

Related to #103
This commit is contained in:
Sebastian Thiel 2015-05-05 14:27:40 +02:00 committed by Kevin K
parent 06e869b518
commit 52a0b8505c
2 changed files with 28 additions and 7 deletions

View file

@ -40,6 +40,11 @@ USAGE:
\tclaptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
For more information try --help'''
_arg_dym_usage = '''The argument --optio is unknown. Did you mean --option ?
USAGE:
\tclaptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
For more information try --help'''
_excluded = '''The argument '--flag' cannot be used with '-F'
USAGE:
\tclaptests [positional2] -F --long-option-2 <option2>
@ -218,6 +223,7 @@ cmds = {'help short: ': ['{} -h'.format(_bin), _help],
'mult_valsmo x2-1: ': ['{} --multvalsmo some other --multvalsmo some'.format(_bin), _mult_vals_2m1],
'mult_valsmo x1: ': ['{} --multvalsmo some other'.format(_bin), _exact],
'F2(ss),O(s),P: ': ['{} value -f -f -o some'.format(_bin), _f2op],
'arg dym: ': ['{} --optio=foo'.format(_bin), _arg_dym_usage],
'O2(ll)P: ': ['{} value --option some --option other'.format(_bin), _o2p],
'O2(l=l=)P: ': ['{} value --option=some --option=other'.format(_bin), _o2p],
'O2(ss)P: ': ['{} value -o some -o other'.format(_bin), _o2p],

View file

@ -1788,12 +1788,27 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
return None;
}
// Shouldn't reach here
self.report_error(format!("The argument --{} isn't valid", arg),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
// Can't reach here...
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<_>>()));
}
}
unreachable!();
}
@ -1882,7 +1897,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
// Didn't match a flag or option, must be invalid
self.report_error( format!("The argument -{} isn't valid",arg_c),
self.report_error(format!("The argument -{} isn't valid",arg_c),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));