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.
This commit is contained in:
Haim Ashkenazi
2018-02-25 08:20:34 +02:00
committed by Steve Francia
parent 7e2436b79d
commit a15d099018
2 changed files with 19 additions and 7 deletions

View File

@ -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 == ""
}