Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete aliases for subcommands #1856

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,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
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,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",
Expand Down