Skip to content

Commit

Permalink
feat(did-you-mean): for long flags (i.e. --long)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Byron authored and kbknapp committed May 5, 2015
1 parent 06e869b commit 52a0b85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions clap-tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
\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>
Expand Down Expand Up @@ -218,6 +223,7 @@
'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],
Expand Down
29 changes: 22 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
}

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

0 comments on commit 52a0b85

Please sign in to comment.