mirror of
https://github.com/spf13/cobra
synced 2024-12-28 07:17:06 +00:00
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:
parent
91e80cc4a4
commit
7ce08e227e
2 changed files with 21 additions and 2 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue