From 7ce08e227e8c9db9b243dfef1e792af31d19cdd0 Mon Sep 17 00:00:00 2001 From: Haim Ashkenazi Date: Sun, 4 Mar 2018 23:56:31 +0200 Subject: [PATCH] zsh-completion: completion should always parse the root command! It was running on the command it was invoked from which caused some additional helpers (--help, --version) not to be generated. --- zsh_completions.go | 6 ++++-- zsh_completions_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/zsh_completions.go b/zsh_completions.go index 2e0d3e38..4705a905 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 c4e1a95f..66b6e692 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 {