Compare commits

...

3 commits

Author SHA1 Message Date
Ingo Bürk 07cc3c5894
Merge ce30e98be2 into 5a1acea321 2024-05-08 14:14:34 +08:00
dependabot[bot] 5a1acea321
build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.3 to 2.0.4 (#2127) 2024-04-13 02:21:03 +00:00
Ingo Bürk ce30e98be2 complete aliases for subcommands
When completing a subcommand, also take its aliases into consideration
instead of only its name.

fixes #1852
2022-11-15 16:21:11 +01:00
4 changed files with 77 additions and 4 deletions

View file

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

View file

@ -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
View file

@ -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
View file

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