mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
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:
parent
06e869b518
commit
52a0b8505c
2 changed files with 28 additions and 7 deletions
|
@ -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],
|
||||
|
|
19
src/app.rs
19
src/app.rs
|
@ -1788,12 +1788,27 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
|
|||
return None;
|
||||
}
|
||||
|
||||
// Shouldn'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<_>>()));
|
||||
// Can't reach here...
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue