Skip to content

Commit 955bc3e

Browse files
authored
Merge pull request urfave#1896 from bartekpacia/fix/autocomplete
Make it possible to `source` the Zsh autocomplete script
2 parents 38003d1 + 9835d74 commit 955bc3e

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

autocomplete/zsh_autocomplete

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
#compdef $PROG
1+
#compdef program
2+
compdef _program program
23

3-
_cli_zsh_autocomplete() {
4-
local -a opts
5-
local cur
6-
cur=${words[-1]}
7-
if [[ "$cur" == "-"* ]]; then
8-
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
9-
else
10-
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
11-
fi
4+
# Replace all occurences of "program" in this file with the actual name of your
5+
# CLI program. We recommend using Find+Replace feature of your editor. Let's say
6+
# your CLI program is called "acme", then replace like so:
7+
# * program => acme
8+
# * _program => _acme
129

13-
if [[ "${opts[1]}" != "" ]]; then
14-
_describe 'values' opts
15-
else
16-
_files
17-
fi
10+
_program() {
11+
local -a opts
12+
local cur
13+
cur=${words[-1]}
14+
if [[ "$cur" == "-"* ]]; then
15+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
16+
else
17+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
18+
fi
19+
20+
if [[ "${opts[1]}" != "" ]]; then
21+
_describe 'values' opts
22+
else
23+
_files
24+
fi
1825
}
1926

20-
compdef _cli_zsh_autocomplete $PROG
27+
# don't run the completion function when being source-ed or eval-ed
28+
if [ "$funcstack[1]" = "_program" ]; then
29+
_program
30+
fi

0 commit comments

Comments
 (0)