mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
zsh-completion ignores hidden commands and flags :)
This commit is contained in:
parent
a15d099018
commit
f0508c8e76
2 changed files with 81 additions and 4 deletions
|
@ -45,11 +45,11 @@ function {{constructPath .}} {
|
||||||
{{end}} "1: :({{subCmdList .}})" \
|
{{end}} "1: :({{subCmdList .}})" \
|
||||||
"*::arg:->args"
|
"*::arg:->args"
|
||||||
|
|
||||||
case $line[1] in {{- range .Commands}}
|
case $line[1] in {{- range .Commands}}{{if not .Hidden}}
|
||||||
{{cmdName .}})
|
{{cmdName .}})
|
||||||
{{constructPath .}}
|
{{constructPath .}}
|
||||||
;;
|
;;
|
||||||
{{end}} esac
|
{{end}}{{end}} esac
|
||||||
}
|
}
|
||||||
{{range .Commands}}
|
{{range .Commands}}
|
||||||
{{template "selectCmdTemplate" .}}
|
{{template "selectCmdTemplate" .}}
|
||||||
|
@ -69,8 +69,10 @@ function {{constructPath .}} {
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
{{define "selectCmdTemplate" -}}
|
{{define "selectCmdTemplate" -}}
|
||||||
|
{{if .Hidden}}{{/* ignore hidden*/}}{{else -}}
|
||||||
{{if .Commands}}{{template "argumentsC" .}}{{else}}{{template "arguments" .}}{{end}}
|
{{if .Commands}}{{template "argumentsC" .}}{{else}}{{template "arguments" .}}{{end}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
{{define "Main" -}}
|
{{define "Main" -}}
|
||||||
#compdef _{{cmdName .}} {{cmdName .}}
|
#compdef _{{cmdName .}} {{cmdName .}}
|
||||||
|
@ -126,6 +128,9 @@ func subCmdList(c *Command) string {
|
||||||
var subCmds []string
|
var subCmds []string
|
||||||
|
|
||||||
for _, cmd := range c.Commands() {
|
for _, cmd := range c.Commands() {
|
||||||
|
if cmd.Hidden {
|
||||||
|
continue
|
||||||
|
}
|
||||||
subCmds = append(subCmds, cmd.Name())
|
subCmds = append(subCmds, cmd.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,10 +140,14 @@ func subCmdList(c *Command) string {
|
||||||
func extractFlags(c *Command) []*pflag.Flag {
|
func extractFlags(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 {
|
||||||
flags = append(flags, f)
|
flags = append(flags, f)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
c.InheritedFlags().VisitAll(func(f *pflag.Flag) {
|
c.InheritedFlags().VisitAll(func(f *pflag.Flag) {
|
||||||
|
if !f.Hidden {
|
||||||
flags = append(flags, f)
|
flags = append(flags, f)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return flags
|
return flags
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -114,7 +115,74 @@ func TestGenZshCompletion(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenZshCompletionHidden(t *testing.T) {
|
||||||
|
tcs := []struct {
|
||||||
|
name string
|
||||||
|
root *Command
|
||||||
|
expectedExpressions []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "hidden commmands",
|
||||||
|
root: func() *Command {
|
||||||
|
r := &Command{
|
||||||
|
Use: "main",
|
||||||
|
Short: "main short description",
|
||||||
|
}
|
||||||
|
s1 := &Command{
|
||||||
|
Use: "sub1",
|
||||||
|
Hidden: true,
|
||||||
|
Run: emptyRun,
|
||||||
|
}
|
||||||
|
s2 := &Command{
|
||||||
|
Use: "sub2",
|
||||||
|
Short: "short sub2 description",
|
||||||
|
Run: emptyRun,
|
||||||
|
}
|
||||||
|
r.AddCommand(s1, s2)
|
||||||
|
|
||||||
|
return r
|
||||||
|
}(),
|
||||||
|
expectedExpressions: []string{
|
||||||
|
"sub1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "hidden flags",
|
||||||
|
root: func() *Command {
|
||||||
|
var hidden string
|
||||||
|
r := &Command{
|
||||||
|
Use: "root",
|
||||||
|
Short: "root short description",
|
||||||
|
Run: emptyRun,
|
||||||
|
}
|
||||||
|
r.Flags().StringVarP(&hidden, "hidden", "H", hidden, "hidden usage")
|
||||||
|
if err := r.Flags().MarkHidden("hidden"); err != nil {
|
||||||
|
t.Errorf("Error setting flag hidden: %v\n", err)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}(),
|
||||||
|
expectedExpressions: []string{
|
||||||
|
"--hidden",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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.String()
|
||||||
|
|
||||||
|
for _, expr := range tc.expectedExpressions {
|
||||||
|
if strings.Contains(output, expr) {
|
||||||
|
t.Errorf("Expected completion (%s) not to contain '%s' but it does", output, expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkConstructPath(b *testing.B) {
|
func BenchmarkConstructPath(b *testing.B) {
|
||||||
|
|
Loading…
Reference in a new issue