Skip to content

Commit

Permalink
imp(Completions): allows matching on possible values in options
Browse files Browse the repository at this point in the history
Now when one completes an option with possible values, those values will be displayed. Imagine
a program with an `--editor` option, which accepts either `vi`, or `emacs`. The following would
be displayed for completions

```
$ prog --editor <tab><tab>
vi emacs
```

Closes #557
  • Loading branch information
kbknapp committed Jul 1, 2016
1 parent 70fa5f7 commit 89cc202
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ complete -F _{name} {name}
if let Some(l) = o.long {
opts = format!("{}
--{})
COMPREPLY=(\"{}\")
COMPREPLY=({})
return 0
;;", opts, l, vals_for(o));
}
if let Some(s) = o.short {
opts = format!("{}
-{})
COMPREPLY=(\"{}\")
COMPREPLY=({})
return 0
;;", opts, s, vals_for(o));
}
}
Expand Down Expand Up @@ -224,7 +226,11 @@ pub fn get_all_subcommand_paths(p: &Parser, first: bool) -> Vec<String> {
fn vals_for(o: &OptBuilder) -> String {
use args::AnyArg;
let mut ret = String::new();
if let Some(ref vec) = o.val_names() {
let mut needs_quotes = true;
if let Some(ref vals) = o.possible_vals() {
needs_quotes = false;
ret = format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "));
} else if let Some(ref vec) = o.val_names() {
let mut it = vec.iter().peekable();
while let Some((_, val)) = it.next() {
ret = format!("{}<{}>{}", ret, val,
Expand Down Expand Up @@ -257,5 +263,8 @@ fn vals_for(o: &OptBuilder) -> String {
ret = format!("{}...", ret);
}
}
if needs_quotes {
ret = format!("\"{}\"", ret);
}
ret
}

0 comments on commit 89cc202

Please sign in to comment.