-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1896 from bartekpacia/fix/autocomplete
Make it possible to `source` the Zsh autocomplete script
- Loading branch information
Showing
1 changed file
with
26 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
#compdef $PROG | ||
#compdef program | ||
compdef _program program | ||
|
||
_cli_zsh_autocomplete() { | ||
local -a opts | ||
local cur | ||
cur=${words[-1]} | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}") | ||
else | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}") | ||
fi | ||
# Replace all occurences of "program" in this file with the actual name of your | ||
# CLI program. We recommend using Find+Replace feature of your editor. Let's say | ||
# your CLI program is called "acme", then replace like so: | ||
# * program => acme | ||
# * _program => _acme | ||
|
||
if [[ "${opts[1]}" != "" ]]; then | ||
_describe 'values' opts | ||
else | ||
_files | ||
fi | ||
_program() { | ||
local -a opts | ||
local cur | ||
cur=${words[-1]} | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}") | ||
else | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}") | ||
fi | ||
|
||
if [[ "${opts[1]}" != "" ]]; then | ||
_describe 'values' opts | ||
else | ||
_files | ||
fi | ||
} | ||
|
||
compdef _cli_zsh_autocomplete $PROG | ||
# don't run the completion function when being source-ed or eval-ed | ||
if [ "$funcstack[1]" = "_program" ]; then | ||
_program | ||
fi |