mirror of
https://github.com/spf13/cobra
synced 2024-05-20 03:36:41 +00:00
Compare commits
3 commits
fdf0e4be76
...
07cc3c5894
Author | SHA1 | Date | |
---|---|---|---|
07cc3c5894 | |||
5a1acea321 | |||
ce30e98be2 |
|
@ -461,10 +461,20 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
|
|||
// - there are no local, non-persistent flags on the command-line or TraverseChildren is true
|
||||
for _, subCmd := range finalCmd.Commands() {
|
||||
if subCmd.IsAvailableCommand() || subCmd == finalCmd.helpCommand {
|
||||
directive = ShellCompDirectiveNoFileComp
|
||||
|
||||
// Only ever complete the name OR one of the aliases, no need to offer multiple matching ones
|
||||
// for the same command.
|
||||
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
||||
completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
|
||||
} else {
|
||||
for _, alias := range subCmd.Aliases {
|
||||
if strings.HasPrefix(alias, toComplete) {
|
||||
completions = append(completions, fmt.Sprintf("%s\t%s", alias, subCmd.Short))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
directive = ShellCompDirectiveNoFileComp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2287,6 +2287,69 @@ func TestValidArgsNotValidArgsFunc(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommandAliasesCompletionInGo(t *testing.T) {
|
||||
rootCmd := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
}
|
||||
|
||||
subCmd := &Command{
|
||||
Use: "sandstone",
|
||||
Aliases: []string{"slate", "pumice", "pegmatite"},
|
||||
Run: emptyRun,
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(subCmd)
|
||||
|
||||
testcases := []struct {
|
||||
desc string
|
||||
toComplete string
|
||||
expectedCompletion string
|
||||
}{
|
||||
{
|
||||
desc: "command name",
|
||||
toComplete: "sand",
|
||||
expectedCompletion: "sandstone",
|
||||
},
|
||||
{
|
||||
desc: "command name if an alias also matches",
|
||||
toComplete: "s",
|
||||
expectedCompletion: "sandstone",
|
||||
},
|
||||
{
|
||||
desc: "alias if command name does not match",
|
||||
toComplete: "sla",
|
||||
expectedCompletion: "slate",
|
||||
},
|
||||
{
|
||||
desc: "only one alias if multiple match",
|
||||
toComplete: "p",
|
||||
expectedCompletion: "pumice",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
args := append([]string{ShellCompRequestCmd}, tc.toComplete)
|
||||
output, err := executeCommand(rootCmd, args...)
|
||||
|
||||
expectedCompletion := strings.Join([]string{
|
||||
tc.expectedCompletion,
|
||||
":4",
|
||||
"Completion ended with directive: ShellCompDirectiveNoFileComp",
|
||||
"",
|
||||
}, "\n")
|
||||
|
||||
switch {
|
||||
case err == nil && output != expectedCompletion:
|
||||
t.Errorf("expected: %q, got: %q", expectedCompletion, output)
|
||||
case err != nil:
|
||||
t.Errorf("Unexpected error %q", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestArgAliasesCompletionInGo(t *testing.T) {
|
||||
rootCmd := &Command{
|
||||
Use: "root",
|
||||
|
|
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module github.com/spf13/cobra
|
|||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4
|
||||
github.com/inconshreveable/mousetrap v1.1.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,5 @@
|
|||
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
|
|
Loading…
Reference in a new issue