From a15d0990180cf0d99905ad41201211f8ac3cab53 Mon Sep 17 00:00:00 2001 From: Haim Ashkenazi Date: Sun, 25 Feb 2018 08:20:34 +0200 Subject: [PATCH] zsh-completion fixed reference to cmd name cmd.Use is not the command name :). Found it once I figured out that I need to execute the command in order to fully test the generated completion. --- zsh_completions.go | 16 +++++++++++----- zsh_completions_test.go | 10 ++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/zsh_completions.go b/zsh_completions.go index 9bdec65..d1c9e07 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -15,6 +15,7 @@ var ( "constructPath": constructPath, "subCmdList": subCmdList, "extractFlags": extractFlags, + "cmdName": cmdName, "simpleFlag": simpleFlag, } zshCompletionText = ` @@ -45,7 +46,7 @@ function {{constructPath .}} { "*::arg:->args" case $line[1] in {{- range .Commands}} - {{.Use}}) + {{cmdName .}}) {{constructPath .}} ;; {{end}} esac @@ -72,7 +73,7 @@ function {{constructPath .}} { {{- end}} {{define "Main" -}} -#compdef _{{.Use}} {{.Use}} +#compdef _{{cmdName .}} {{cmdName .}} {{template "selectCmdTemplate" .}} {{end}} @@ -102,14 +103,14 @@ func (c *Command) GenZshCompletion(w io.Writer) error { func constructPath(c *Command) string { var path []string tmpCmd := c - path = append(path, tmpCmd.Use) + path = append(path, tmpCmd.Name()) for { if !tmpCmd.HasParent() { break } tmpCmd = tmpCmd.Parent() - path = append(path, tmpCmd.Use) + path = append(path, tmpCmd.Name()) } // reverse path @@ -125,7 +126,7 @@ func subCmdList(c *Command) string { var subCmds []string for _, cmd := range c.Commands() { - subCmds = append(subCmds, cmd.Use) + subCmds = append(subCmds, cmd.Name()) } return strings.Join(subCmds, " ") @@ -142,6 +143,11 @@ func extractFlags(c *Command) []*pflag.Flag { return flags } +// cmdName returns the command's innvocation +func cmdName(c *Command) string { + return c.Name() +} + func simpleFlag(p *pflag.Flag) bool { return p.Name == "" || p.Shorthand == "" } diff --git a/zsh_completions_test.go b/zsh_completions_test.go index 1605629..4d29e54 100644 --- a/zsh_completions_test.go +++ b/zsh_completions_test.go @@ -21,12 +21,13 @@ func TestGenZshCompletion(t *testing.T) { r := &Command{ Use: "mycommand", Long: "My Command long description", + Run: emptyRun, } r.Flags().BoolVar(&debug, "debug", debug, "description") return r }(), expectedExpressions: []string{ - `function _mycommand {\s+_arguments \\\s+"--debug\[description\]"\s+}`, + `(?s)function _mycommand {\s+_arguments \\\s+"--debug\[description\]".*--help.*}`, "#compdef _mycommand mycommand", }, }, @@ -36,6 +37,7 @@ func TestGenZshCompletion(t *testing.T) { r := &Command{ Use: "testcmd", Long: "long description", + Run: emptyRun, } r.Flags().BoolVarP(&debug, "debug", "d", debug, "debug description") return r @@ -54,10 +56,12 @@ func TestGenZshCompletion(t *testing.T) { d := &Command{ Use: "subcmd1", Short: "Subcmd1 short descrition", + Run: emptyRun, } e := &Command{ Use: "subcmd2", Long: "Subcmd2 short description", + Run: emptyRun, } r.PersistentFlags().BoolVar(&debug, "debug", debug, "description") d.Flags().StringVarP(&option, "option", "o", option, "option description") @@ -65,7 +69,7 @@ func TestGenZshCompletion(t *testing.T) { return r }(), expectedExpressions: []string{ - `\\\n\s+"1: :\(subcmd1 subcmd2\)" \\\n`, + `\\\n\s+"1: :\(help subcmd1 subcmd2\)" \\\n`, `_arguments \\\n.*"--debug\[description]"`, `_arguments -C \\\n.*"--debug\[description]"`, `function _rootcmd_subcmd1 {`, @@ -80,6 +84,7 @@ func TestGenZshCompletion(t *testing.T) { r := &Command{ Use: "mycmd", Short: "my command short description", + Run: emptyRun, } r.Flags().StringVarP(&file, "config", "c", file, "config file") r.MarkFlagFilename("config", "ext") @@ -93,6 +98,7 @@ func TestGenZshCompletion(t *testing.T) { for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { + tc.root.Execute() buf := new(bytes.Buffer) tc.root.GenZshCompletion(buf) output := buf.Bytes()