Merge pull request #1350 from urfave/applying-pr1218
Another approach for zsh completion (#1218)
This commit is contained in:
commit
f528cf0a1e
@ -228,6 +228,7 @@ func ExampleApp_Run_subcommandNoAction() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleApp_Run_bashComplete_withShortFlag() {
|
func ExampleApp_Run_bashComplete_withShortFlag() {
|
||||||
|
os.Setenv("SHELL", "bash")
|
||||||
os.Args = []string{"greet", "-", "--generate-bash-completion"}
|
os.Args = []string{"greet", "-", "--generate-bash-completion"}
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
@ -255,6 +256,7 @@ func ExampleApp_Run_bashComplete_withShortFlag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleApp_Run_bashComplete_withLongFlag() {
|
func ExampleApp_Run_bashComplete_withLongFlag() {
|
||||||
|
os.Setenv("SHELL", "bash")
|
||||||
os.Args = []string{"greet", "--s", "--generate-bash-completion"}
|
os.Args = []string{"greet", "--s", "--generate-bash-completion"}
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
@ -283,6 +285,7 @@ func ExampleApp_Run_bashComplete_withLongFlag() {
|
|||||||
// --similar-flag
|
// --similar-flag
|
||||||
}
|
}
|
||||||
func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
|
func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
|
||||||
|
os.Setenv("SHELL", "bash")
|
||||||
os.Args = []string{"greet", "--st", "--generate-bash-completion"}
|
os.Args = []string{"greet", "--st", "--generate-bash-completion"}
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
@ -315,7 +318,7 @@ func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleApp_Run_bashComplete() {
|
func ExampleApp_Run_bashComplete() {
|
||||||
// set args for examples sake
|
os.Setenv("SHELL", "bash")
|
||||||
os.Args = []string{"greet", "--generate-bash-completion"}
|
os.Args = []string{"greet", "--generate-bash-completion"}
|
||||||
|
|
||||||
app := &App{
|
app := &App{
|
||||||
@ -355,7 +358,7 @@ func ExampleApp_Run_bashComplete() {
|
|||||||
func ExampleApp_Run_zshComplete() {
|
func ExampleApp_Run_zshComplete() {
|
||||||
// set args for examples sake
|
// set args for examples sake
|
||||||
os.Args = []string{"greet", "--generate-bash-completion"}
|
os.Args = []string{"greet", "--generate-bash-completion"}
|
||||||
_ = os.Setenv("_CLI_ZSH_AUTOCOMPLETE_HACK", "1")
|
_ = os.Setenv("SHELL", "/usr/bin/zsh")
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
app.Name = "greet"
|
app.Name = "greet"
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
#compdef $PROG
|
#compdef $PROG
|
||||||
|
|
||||||
_cli_zsh_autocomplete() {
|
_cli_zsh_autocomplete() {
|
||||||
|
|
||||||
local -a opts
|
local -a opts
|
||||||
local cur
|
local cur
|
||||||
cur=${words[-1]}
|
cur=${words[-1]}
|
||||||
if [[ "$cur" == "-"* ]]; then
|
if [[ "$cur" == "-"* ]]; then
|
||||||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
|
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
|
||||||
else
|
else
|
||||||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
|
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${opts[1]}" != "" ]]; then
|
if [[ "${opts[1]}" != "" ]]; then
|
||||||
@ -16,8 +15,6 @@ _cli_zsh_autocomplete() {
|
|||||||
else
|
else
|
||||||
_files
|
_files
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _cli_zsh_autocomplete $PROG
|
compdef _cli_zsh_autocomplete $PROG
|
||||||
|
@ -1165,15 +1165,14 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### ZSH Support
|
#### ZSH Support
|
||||||
Auto-completion for ZSH is also supported using the `autocomplete/zsh_autocomplete`
|
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`.
|
file included in this repo. One environment variable is used, `PROG`. Set
|
||||||
Set `PROG` to the program name as before, set `_CLI_ZSH_AUTOCOMPLETE_HACK` to `1`, and
|
`PROG` to the program name as before, and then `source path/to/autocomplete/zsh_autocomplete`.
|
||||||
then `source path/to/autocomplete/zsh_autocomplete`. Adding the following lines to your ZSH
|
Adding the following lines to your ZSH configuration file (usually `.zshrc`)
|
||||||
configuration file (usually `.zshrc`) will allow the auto-completion to persist across new shells:
|
will allow the auto-completion to persist across new shells:
|
||||||
|
|
||||||
```
|
```
|
||||||
PROG=<myprogram>
|
PROG=<myprogram>
|
||||||
_CLI_ZSH_AUTOCOMPLETE_HACK=1
|
|
||||||
source path/to/autocomplete/zsh_autocomplete
|
source path/to/autocomplete/zsh_autocomplete
|
||||||
```
|
```
|
||||||
#### ZSH default auto-complete example
|
#### ZSH default auto-complete example
|
||||||
|
2
help.go
2
help.go
@ -107,7 +107,7 @@ func printCommandSuggestions(commands []*Command, writer io.Writer) {
|
|||||||
if command.Hidden {
|
if command.Hidden {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK") == "1" {
|
if strings.HasSuffix(os.Getenv("SHELL"), "zsh") {
|
||||||
for _, name := range command.Names() {
|
for _, name := range command.Names() {
|
||||||
_, _ = fmt.Fprintf(writer, "%s:%s\n", name, command.Usage)
|
_, _ = fmt.Fprintf(writer, "%s:%s\n", name, command.Usage)
|
||||||
}
|
}
|
||||||
|
@ -1040,12 +1040,16 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultCompleteWithFlags(t *testing.T) {
|
func TestDefaultCompleteWithFlags(t *testing.T) {
|
||||||
|
origEnv := os.Environ()
|
||||||
origArgv := os.Args
|
origArgv := os.Args
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
os.Args = origArgv
|
os.Args = origArgv
|
||||||
|
resetEnv(origEnv)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
os.Setenv("SHELL", "bash")
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
c *Context
|
c *Context
|
||||||
|
Loading…
Reference in New Issue
Block a user