forked from mirror/cobra
zsh-completion: tidy up function and variable names
There are many files in the package, renamed all zsh-completion related names to convey that.
This commit is contained in:
parent
8822449c0f
commit
d262154093
|
@ -11,10 +11,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
funcMap = template.FuncMap{
|
zshCompFuncMap = template.FuncMap{
|
||||||
"genZshFuncName": generateZshCompletionFuncName,
|
"genZshFuncName": zshCompGenFuncName,
|
||||||
"extractFlags": extractFlags,
|
"extractFlags": zshCompExtractFlag,
|
||||||
"genFlagEntryForZshArguments": genFlagEntryForZshArguments,
|
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
|
||||||
}
|
}
|
||||||
zshCompletionText = `
|
zshCompletionText = `
|
||||||
{{/* should accept Command (that contains subcommands) as parameter */}}
|
{{/* should accept Command (that contains subcommands) as parameter */}}
|
||||||
|
@ -88,21 +88,21 @@ func (c *Command) GenZshCompletionFile(filename string) error {
|
||||||
// writer. The completion always run on the root command regardless of the
|
// writer. The completion always run on the root command regardless of the
|
||||||
// command it was called from.
|
// command it was called from.
|
||||||
func (c *Command) GenZshCompletion(w io.Writer) error {
|
func (c *Command) GenZshCompletion(w io.Writer) error {
|
||||||
tmpl, err := template.New("Main").Funcs(funcMap).Parse(zshCompletionText)
|
tmpl, err := template.New("Main").Funcs(zshCompFuncMap).Parse(zshCompletionText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating zsh completion template: %v", err)
|
return fmt.Errorf("error creating zsh completion template: %v", err)
|
||||||
}
|
}
|
||||||
return tmpl.Execute(w, c.Root())
|
return tmpl.Execute(w, c.Root())
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateZshCompletionFuncName(c *Command) string {
|
func zshCompGenFuncName(c *Command) string {
|
||||||
if c.HasParent() {
|
if c.HasParent() {
|
||||||
return generateZshCompletionFuncName(c.Parent()) + "_" + c.Name()
|
return zshCompGenFuncName(c.Parent()) + "_" + c.Name()
|
||||||
}
|
}
|
||||||
return "_" + c.Name()
|
return "_" + c.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractFlags(c *Command) []*pflag.Flag {
|
func zshCompExtractFlag(c *Command) []*pflag.Flag {
|
||||||
var flags []*pflag.Flag
|
var flags []*pflag.Flag
|
||||||
c.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
c.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
||||||
if !f.Hidden {
|
if !f.Hidden {
|
||||||
|
@ -117,19 +117,19 @@ func extractFlags(c *Command) []*pflag.Flag {
|
||||||
return flags
|
return flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// genFlagEntryForZshArguments returns an entry that matches _arguments
|
// zshCompGenFlagEntryForArguments returns an entry that matches _arguments
|
||||||
// zsh-completion parameters. It's too complicated to generate in a template.
|
// zsh-completion parameters. It's too complicated to generate in a template.
|
||||||
func genFlagEntryForZshArguments(f *pflag.Flag) string {
|
func zshCompGenFlagEntryForArguments(f *pflag.Flag) string {
|
||||||
if f.Name == "" || f.Shorthand == "" {
|
if f.Name == "" || f.Shorthand == "" {
|
||||||
return genFlagEntryForSingleOptionFlag(f)
|
return zshCompGenFlagEntryForSingleOptionFlag(f)
|
||||||
}
|
}
|
||||||
return genFlagEntryForMultiOptionFlag(f)
|
return zshCompGenFlagEntryForMultiOptionFlag(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func genFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
|
func zshCompGenFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
|
||||||
var option, multiMark, extras string
|
var option, multiMark, extras string
|
||||||
|
|
||||||
if flagCouldBeSpecifiedMoreThenOnce(f) {
|
if zshCompFlagCouldBeSpecifiedMoreThenOnce(f) {
|
||||||
multiMark = "*"
|
multiMark = "*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,27 +137,27 @@ func genFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
|
||||||
if option == "--" {
|
if option == "--" {
|
||||||
option = "-" + f.Shorthand
|
option = "-" + f.Shorthand
|
||||||
}
|
}
|
||||||
extras = genZshFlagEntryExtras(f)
|
extras = zshCompGenFlagEntryExtras(f)
|
||||||
|
|
||||||
return fmt.Sprintf(`'%s%s[%s]%s'`, multiMark, option, quoteDescription(f.Usage), extras)
|
return fmt.Sprintf(`'%s%s[%s]%s'`, multiMark, option, zshCompQuoteFlagDescription(f.Usage), extras)
|
||||||
}
|
}
|
||||||
|
|
||||||
func genFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
|
func zshCompGenFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
|
||||||
var options, parenMultiMark, curlyMultiMark, extras string
|
var options, parenMultiMark, curlyMultiMark, extras string
|
||||||
|
|
||||||
if flagCouldBeSpecifiedMoreThenOnce(f) {
|
if zshCompFlagCouldBeSpecifiedMoreThenOnce(f) {
|
||||||
parenMultiMark = "*"
|
parenMultiMark = "*"
|
||||||
curlyMultiMark = "\\*"
|
curlyMultiMark = "\\*"
|
||||||
}
|
}
|
||||||
|
|
||||||
options = fmt.Sprintf(`'(%s-%s %s--%s)'{%s-%s,%s--%s}`,
|
options = fmt.Sprintf(`'(%s-%s %s--%s)'{%s-%s,%s--%s}`,
|
||||||
parenMultiMark, f.Shorthand, parenMultiMark, f.Name, curlyMultiMark, f.Shorthand, curlyMultiMark, f.Name)
|
parenMultiMark, f.Shorthand, parenMultiMark, f.Name, curlyMultiMark, f.Shorthand, curlyMultiMark, f.Name)
|
||||||
extras = genZshFlagEntryExtras(f)
|
extras = zshCompGenFlagEntryExtras(f)
|
||||||
|
|
||||||
return fmt.Sprintf(`%s'[%s]%s'`, options, quoteDescription(f.Usage), extras)
|
return fmt.Sprintf(`%s'[%s]%s'`, options, zshCompQuoteFlagDescription(f.Usage), extras)
|
||||||
}
|
}
|
||||||
|
|
||||||
func genZshFlagEntryExtras(f *pflag.Flag) string {
|
func zshCompGenFlagEntryExtras(f *pflag.Flag) string {
|
||||||
var extras string
|
var extras string
|
||||||
|
|
||||||
globs, pathSpecified := f.Annotations[BashCompFilenameExt]
|
globs, pathSpecified := f.Annotations[BashCompFilenameExt]
|
||||||
|
@ -173,11 +173,11 @@ func genZshFlagEntryExtras(f *pflag.Flag) string {
|
||||||
return extras
|
return extras
|
||||||
}
|
}
|
||||||
|
|
||||||
func flagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
|
func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
|
||||||
return strings.Contains(f.Value.Type(), "Slice") ||
|
return strings.Contains(f.Value.Type(), "Slice") ||
|
||||||
strings.Contains(f.Value.Type(), "Array")
|
strings.Contains(f.Value.Type(), "Array")
|
||||||
}
|
}
|
||||||
|
|
||||||
func quoteDescription(s string) string {
|
func zshCompQuoteFlagDescription(s string) string {
|
||||||
return strings.Replace(s, "'", `'\''`, -1)
|
return strings.Replace(s, "'", `'\''`, -1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -285,8 +285,8 @@ func TestExtractFlags(t *testing.T) {
|
||||||
d.Flags().BoolVar(&cmdd, "cmd-d", cmdd, "Command D")
|
d.Flags().BoolVar(&cmdd, "cmd-d", cmdd, "Command D")
|
||||||
c.AddCommand(d)
|
c.AddCommand(d)
|
||||||
|
|
||||||
resC := extractFlags(c)
|
resC := zshCompExtractFlag(c)
|
||||||
resD := extractFlags(d)
|
resD := zshCompExtractFlag(d)
|
||||||
|
|
||||||
if len(resC) != 2 {
|
if len(resC) != 2 {
|
||||||
t.Errorf("expected Command C to return 2 flags, got %d", len(resC))
|
t.Errorf("expected Command C to return 2 flags, got %d", len(resC))
|
||||||
|
|
Loading…
Reference in New Issue