2017-07-30 11:42:35 +03:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-09-05 19:32:32 +03:00
|
|
|
"os"
|
2018-02-28 18:04:17 +03:00
|
|
|
"strings"
|
2018-02-24 19:53:13 +03:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
funcMap = template.FuncMap{
|
2018-02-28 18:04:17 +03:00
|
|
|
"genZshFuncName": generateZshCompletionFuncName,
|
|
|
|
"extractFlags": extractFlags,
|
|
|
|
"genFlagEntryForZshArguments": genFlagEntryForZshArguments,
|
2018-02-24 19:53:13 +03:00
|
|
|
}
|
|
|
|
zshCompletionText = `
|
|
|
|
{{/* should accept Command (that contains subcommands) as parameter */}}
|
|
|
|
{{define "argumentsC" -}}
|
2018-02-28 13:49:53 +03:00
|
|
|
{{ $cmdPath := genZshFuncName .}}
|
|
|
|
function {{$cmdPath}} {
|
2018-02-26 23:31:06 +03:00
|
|
|
local -a commands
|
2018-02-24 19:53:13 +03:00
|
|
|
|
2018-02-26 23:31:06 +03:00
|
|
|
_arguments -C \{{- range extractFlags .}}
|
2018-02-28 18:04:17 +03:00
|
|
|
{{genFlagEntryForZshArguments .}} \{{- end}}
|
2018-02-26 23:31:06 +03:00
|
|
|
"1: :->cmnds" \
|
2018-02-24 19:53:13 +03:00
|
|
|
"*::arg:->args"
|
|
|
|
|
2018-02-26 23:31:06 +03:00
|
|
|
case $state in
|
|
|
|
cmnds)
|
|
|
|
commands=({{range .Commands}}{{if not .Hidden}}
|
2018-02-28 13:49:53 +03:00
|
|
|
"{{.Name}}:{{.Short}}"{{end}}{{end}}
|
2018-02-26 23:31:06 +03:00
|
|
|
)
|
|
|
|
_describe "command" commands
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case "$words[1]" in {{- range .Commands}}{{if not .Hidden}}
|
2018-02-28 13:49:53 +03:00
|
|
|
{{.Name}})
|
|
|
|
{{$cmdPath}}_{{.Name}}
|
2018-02-26 23:31:06 +03:00
|
|
|
;;{{end}}{{end}}
|
|
|
|
esac
|
2018-02-24 19:53:13 +03:00
|
|
|
}
|
2018-02-26 23:31:06 +03:00
|
|
|
{{range .Commands}}{{if not .Hidden}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{template "selectCmdTemplate" .}}
|
2018-02-26 23:31:06 +03:00
|
|
|
{{- end}}{{end}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
{{/* should accept Command without subcommands as parameter */}}
|
|
|
|
{{define "arguments" -}}
|
2018-02-28 13:49:53 +03:00
|
|
|
function {{genZshFuncName .}} {
|
2018-02-26 23:31:06 +03:00
|
|
|
{{" _arguments"}}{{range extractFlags .}} \
|
2018-02-28 18:04:17 +03:00
|
|
|
{{genFlagEntryForZshArguments . -}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{end}}
|
|
|
|
}
|
2018-02-26 23:31:06 +03:00
|
|
|
{{end}}
|
2018-02-24 19:53:13 +03:00
|
|
|
|
2018-02-26 23:31:06 +03:00
|
|
|
{{/* dispatcher for commands with or without subcommands */}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{define "selectCmdTemplate" -}}
|
2018-02-25 15:12:58 +03:00
|
|
|
{{if .Hidden}}{{/* ignore hidden*/}}{{else -}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{if .Commands}}{{template "argumentsC" .}}{{else}}{{template "arguments" .}}{{end}}
|
|
|
|
{{- end}}
|
2018-02-25 15:12:58 +03:00
|
|
|
{{- end}}
|
2018-02-24 19:53:13 +03:00
|
|
|
|
2018-02-26 23:31:06 +03:00
|
|
|
{{/* template entry point */}}
|
2018-02-24 19:53:13 +03:00
|
|
|
{{define "Main" -}}
|
2018-02-28 13:49:53 +03:00
|
|
|
#compdef _{{.Name}} {{.Name}}
|
2018-02-24 19:53:13 +03:00
|
|
|
|
|
|
|
{{template "selectCmdTemplate" .}}
|
|
|
|
{{end}}
|
|
|
|
`
|
2017-07-30 11:42:35 +03:00
|
|
|
)
|
|
|
|
|
2017-09-05 19:32:32 +03:00
|
|
|
// GenZshCompletionFile generates zsh completion file.
|
2017-09-05 20:20:51 +03:00
|
|
|
func (c *Command) GenZshCompletionFile(filename string) error {
|
2017-09-05 19:32:32 +03:00
|
|
|
outFile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2017-09-05 20:20:51 +03:00
|
|
|
return c.GenZshCompletion(outFile)
|
2017-09-05 19:32:32 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 11:42:35 +03:00
|
|
|
// GenZshCompletion generates a zsh completion file and writes to the passed writer.
|
2017-09-05 20:20:51 +03:00
|
|
|
func (c *Command) GenZshCompletion(w io.Writer) error {
|
2018-02-24 19:53:13 +03:00
|
|
|
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)
|
2017-07-30 11:42:35 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:49:53 +03:00
|
|
|
func generateZshCompletionFuncName(c *Command) string {
|
|
|
|
if c.HasParent() {
|
|
|
|
return generateZshCompletionFuncName(c.Parent()) + "_" + c.Name()
|
2017-07-30 11:42:35 +03:00
|
|
|
}
|
2018-02-28 13:49:53 +03:00
|
|
|
return "_" + c.Name()
|
2017-07-30 11:42:35 +03:00
|
|
|
}
|
|
|
|
|
2018-02-24 19:53:13 +03:00
|
|
|
func extractFlags(c *Command) []*pflag.Flag {
|
|
|
|
var flags []*pflag.Flag
|
|
|
|
c.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
2018-02-25 15:12:58 +03:00
|
|
|
if !f.Hidden {
|
|
|
|
flags = append(flags, f)
|
|
|
|
}
|
2018-02-24 19:53:13 +03:00
|
|
|
})
|
|
|
|
c.InheritedFlags().VisitAll(func(f *pflag.Flag) {
|
2018-02-25 15:12:58 +03:00
|
|
|
if !f.Hidden {
|
|
|
|
flags = append(flags, f)
|
|
|
|
}
|
2018-02-24 19:53:13 +03:00
|
|
|
})
|
|
|
|
return flags
|
2017-07-30 11:42:35 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:04:17 +03:00
|
|
|
// genFlagEntryForZshArguments returns an entry that matches _arguments
|
|
|
|
// zsh-completion parameters. It's too complicated to generate in a template.
|
|
|
|
func genFlagEntryForZshArguments(f *pflag.Flag) string {
|
|
|
|
if f.Name == "" || f.Shorthand == "" {
|
|
|
|
return genFlagEntryForSingleOptionFlag(f)
|
|
|
|
}
|
|
|
|
return genFlagEntryForMultiOptionFlag(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func genFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
|
|
|
|
var option, multiMark, extras string
|
|
|
|
|
2018-02-28 22:52:35 +03:00
|
|
|
if flagCouldBeSpecifiedMoreThenOnce(f) {
|
2018-02-28 18:04:17 +03:00
|
|
|
multiMark = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
option = "--" + f.Name
|
|
|
|
if option == "--" {
|
|
|
|
option = "-" + f.Shorthand
|
|
|
|
}
|
|
|
|
extras = genZshFlagEntryExtras(f)
|
|
|
|
|
|
|
|
return fmt.Sprintf(`"%s%s[%s]%s"`, multiMark, option, f.Usage, extras)
|
|
|
|
}
|
|
|
|
|
|
|
|
func genFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
|
|
|
|
var options, parenMultiMark, curlyMultiMark, extras string
|
|
|
|
|
2018-02-28 22:52:35 +03:00
|
|
|
if flagCouldBeSpecifiedMoreThenOnce(f) {
|
2018-02-28 18:04:17 +03:00
|
|
|
parenMultiMark = "*"
|
|
|
|
curlyMultiMark = "\\*"
|
|
|
|
}
|
|
|
|
|
|
|
|
options = fmt.Sprintf(`"(%s-%s %s--%s)"{%s-%s,%s--%s}`,
|
|
|
|
parenMultiMark, f.Shorthand, parenMultiMark, f.Name, curlyMultiMark, f.Shorthand, curlyMultiMark, f.Name)
|
|
|
|
extras = genZshFlagEntryExtras(f)
|
|
|
|
|
|
|
|
return fmt.Sprintf(`%s"[%s]%s"`, options, f.Usage, extras)
|
|
|
|
}
|
|
|
|
|
|
|
|
func genZshFlagEntryExtras(f *pflag.Flag) string {
|
|
|
|
var extras string
|
|
|
|
|
|
|
|
_, pathSpecified := f.Annotations[BashCompFilenameExt]
|
|
|
|
if pathSpecified {
|
|
|
|
extras = ":filename:_files"
|
2018-02-28 22:52:35 +03:00
|
|
|
} else if f.NoOptDefVal == "" {
|
2018-02-28 18:04:17 +03:00
|
|
|
extras = ":" // allow option variable without assisting
|
|
|
|
}
|
|
|
|
|
|
|
|
return extras
|
|
|
|
}
|
2018-02-28 22:52:35 +03:00
|
|
|
|
|
|
|
func flagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
|
|
|
|
return strings.Contains(f.Value.Type(), "Slice") ||
|
|
|
|
strings.Contains(f.Value.Type(), "Array")
|
|
|
|
}
|