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.
This commit is contained in:
Haim Ashkenazi 2018-03-04 23:56:31 +02:00 committed by Steve Francia
parent 91e80cc4a4
commit 7ce08e227e
2 changed files with 21 additions and 2 deletions

View file

@ -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 {

View file

@ -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 {