diff --git a/app_test.go b/app_test.go index 5f20078..26ae28a 100644 --- a/app_test.go +++ b/app_test.go @@ -228,6 +228,7 @@ func ExampleApp_Run_subcommandNoAction() { } func ExampleApp_Run_bashComplete_withShortFlag() { + os.Setenv("SHELL", "bash") os.Args = []string{"greet", "-", "--generate-bash-completion"} app := NewApp() @@ -255,6 +256,7 @@ func ExampleApp_Run_bashComplete_withShortFlag() { } func ExampleApp_Run_bashComplete_withLongFlag() { + os.Setenv("SHELL", "bash") os.Args = []string{"greet", "--s", "--generate-bash-completion"} app := NewApp() @@ -283,6 +285,7 @@ func ExampleApp_Run_bashComplete_withLongFlag() { // --similar-flag } func ExampleApp_Run_bashComplete_withMultipleLongFlag() { + os.Setenv("SHELL", "bash") os.Args = []string{"greet", "--st", "--generate-bash-completion"} app := NewApp() @@ -315,7 +318,7 @@ func ExampleApp_Run_bashComplete_withMultipleLongFlag() { } func ExampleApp_Run_bashComplete() { - // set args for examples sake + os.Setenv("SHELL", "bash") os.Args = []string{"greet", "--generate-bash-completion"} app := &App{ diff --git a/autocomplete/zsh_autocomplete b/autocomplete/zsh_autocomplete index ee1b562..b519666 100644 --- a/autocomplete/zsh_autocomplete +++ b/autocomplete/zsh_autocomplete @@ -1,16 +1,20 @@ #compdef $PROG -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 +_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 -if [[ "${opts[1]}" != "" ]]; then - _describe 'values' opts -else - _files -fi + if [[ "${opts[1]}" != "" ]]; then + _describe 'values' opts + else + _files + fi +} + +compdef _cli_zsh_autocomplete $PROG diff --git a/docs/v2/manual.md b/docs/v2/manual.md index 3d96bb8..fd5656a 100644 --- a/docs/v2/manual.md +++ b/docs/v2/manual.md @@ -1165,10 +1165,10 @@ func main() { ``` #### ZSH Support -Auto-completion for ZSH is also supported using the `autocomplete/zsh_autocomplete` -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`) +Auto-completion for ZSH is also supported using the `autocomplete/zsh_autocomplete` +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: ``` diff --git a/help_test.go b/help_test.go index 98530dd..17a263d 100644 --- a/help_test.go +++ b/help_test.go @@ -1040,12 +1040,16 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) { } func TestDefaultCompleteWithFlags(t *testing.T) { + origEnv := os.Environ() origArgv := os.Args t.Cleanup(func() { os.Args = origArgv + resetEnv(origEnv) }) + os.Setenv("SHELL", "bash") + for _, tc := range []struct { name string c *Context