diff --git a/app_test.go b/app_test.go index 651f0f4f5d..6eba310ec4 100644 --- a/app_test.go +++ b/app_test.go @@ -356,7 +356,7 @@ func ExampleApp_Run_bashComplete() { func ExampleApp_Run_zshComplete() { // set args for examples sake os.Args = []string{"greet", "--generate-bash-completion"} - _ = os.Setenv("_CLI_ZSH_AUTOCOMPLETE_HACK", "1") + _ = os.Setenv("SHELL", "/usr/bin/zsh") app := NewApp() app.Name = "greet" diff --git a/autocomplete/zsh_autocomplete b/autocomplete/zsh_autocomplete index cf39c888af..ee1b562743 100644 --- a/autocomplete/zsh_autocomplete +++ b/autocomplete/zsh_autocomplete @@ -1,23 +1,16 @@ #compdef $PROG -_cli_zsh_autocomplete() { +local -a opts +local cur +cur=${words[-1]} +if [[ "$cur" == "-"* ]]; then + opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") +else + opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}") +fi - local -a opts - local cur - cur=${words[-1]} - if [[ "$cur" == "-"* ]]; then - opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") - else - opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") - fi - - if [[ "${opts[1]}" != "" ]]; then - _describe 'values' opts - else - _files - fi - - return -} - -compdef _cli_zsh_autocomplete $PROG +if [[ "${opts[1]}" != "" ]]; then + _describe 'values' opts +else + _files +fi diff --git a/docs/v2/manual.md b/docs/v2/manual.md index af09010b9a..b8b44033a2 100644 --- a/docs/v2/manual.md +++ b/docs/v2/manual.md @@ -1211,14 +1211,13 @@ func main() { #### ZSH Support Auto-completion for ZSH is also supported using the `autocomplete/zsh_autocomplete` -file included in this repo. Two environment variables are used, `PROG` and `_CLI_ZSH_AUTOCOMPLETE_HACK`. -Set `PROG` to the program name as before, set `_CLI_ZSH_AUTOCOMPLETE_HACK` to `1`, and -then `source path/to/autocomplete/zsh_autocomplete`. Adding the following lines to your ZSH -configuration file (usually `.zshrc`) will allow the auto-completion to persist across new shells: +file included in this repo. One environment variable is used, `PROG`. Set +`PROG` to the program name as before, and then `source path/to/autocomplete/zsh_autocomplete`. +Adding the following lines to your ZSH configuration file (usually `.zshrc`) +will allow the auto-completion to persist across new shells: ``` PROG= -_CLI_ZSH_AUTOCOMPLETE_HACK=1 source path/to/autocomplete/zsh_autocomplete ``` #### ZSH default auto-complete example diff --git a/help.go b/help.go index 0a421ee99a..a7b3a944a2 100644 --- a/help.go +++ b/help.go @@ -102,7 +102,7 @@ func printCommandSuggestions(commands []*Command, writer io.Writer) { if command.Hidden { continue } - if os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK") == "1" { + if strings.Contains(os.Getenv("SHELL"), "zsh") { for _, name := range command.Names() { _, _ = fmt.Fprintf(writer, "%s:%s\n", name, command.Usage) }