diff --git a/zsh_completions.go b/zsh_completions.go index 2e0d3e3..4705a90 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -84,13 +84,15 @@ func (c *Command) GenZshCompletionFile(filename string) error { return c.GenZshCompletion(outFile) } -// GenZshCompletion generates a zsh completion file and writes to the passed writer. +// GenZshCompletion generates a zsh completion file and writes to the passed +// writer. The completion always run on the root command regardless of the +// command it was called from. func (c *Command) GenZshCompletion(w io.Writer) error { tmpl, err := template.New("Main").Funcs(funcMap).Parse(zshCompletionText) if err != nil { return fmt.Errorf("error creating zsh completion template: %v", err) } - return tmpl.Execute(w, c) + return tmpl.Execute(w, c.Root()) } func generateZshCompletionFuncName(c *Command) string { diff --git a/zsh_completions_test.go b/zsh_completions_test.go index c4e1a95..66b6e69 100644 --- a/zsh_completions_test.go +++ b/zsh_completions_test.go @@ -111,6 +111,23 @@ func TestGenZshCompletion(t *testing.T) { `'\(\*-d \*--debug\)'{\\\*-d,\\\*--debug}`, }, }, + { + name: "command should run on the root command so --version and --help will be generated", + root: func() *Command { + r := &Command{ + Use: "mycmd", + Short: "mycmd short description", + Version: "myversion", + } + s := genTestCommand("sub1", true) + r.AddCommand(s) + return s + }(), + expectedExpressions: []string{ + "--version", + "--help", + }, + }, } for _, tc := range tcs {