Skip to content

Commit

Permalink
Stop suggesting command and label aliases in shell completion
Browse files Browse the repository at this point in the history
- printCommandSuggestions: omit Command aliases from suggesting
- printFlagSuggestions: omit Flag aliases from suggesting
- fix tests
  • Loading branch information
bartekpacia committed Mar 11, 2024
1 parent 67566bb commit 43272e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
7 changes: 0 additions & 7 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,8 @@ func ExampleApp_Run_bashComplete_withShortFlag() {
_ = app.Run(os.Args)
// Output:
// --other
// -o
// --xyz
// -x
// --help
// -h
}

func ExampleApp_Run_bashComplete_withLongFlag() {
Expand Down Expand Up @@ -360,10 +357,8 @@ func ExampleApp_Run_bashComplete() {
_ = app.Run(os.Args)
// Output:
// describeit
// d
// next
// help
// h
}

func ExampleApp_Run_zshComplete() {
Expand Down Expand Up @@ -398,10 +393,8 @@ func ExampleApp_Run_zshComplete() {
_ = app.Run(os.Args)
// Output:
// describeit:use it to see a description
// d:use it to see a description
// next:next example
// help:Shows a list of commands or help for one command
// h:Shows a list of commands or help for one command
}

func ExampleApp_Run_sliceValues() {
Expand Down
40 changes: 17 additions & 23 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,9 @@ func printCommandSuggestions(commands []*Command, writer io.Writer) {
continue
}
if strings.HasSuffix(os.Getenv("SHELL"), "zsh") {
for _, name := range command.Names() {
_, _ = fmt.Fprintf(writer, "%s:%s\n", name, command.Usage)
}
_, _ = fmt.Fprintf(writer, "%s:%s\n", command.Name, command.Usage)
} else {
for _, name := range command.Names() {
_, _ = fmt.Fprintf(writer, "%s\n", name)
}
_, _ = fmt.Fprintf(writer, "%s\n", command.Name)
}
}
}
Expand Down Expand Up @@ -186,23 +182,21 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
if bflag, ok := flag.(*BoolFlag); ok && bflag.Hidden {
continue
}
for _, name := range flag.Names() {
name = strings.TrimSpace(name)
// this will get total count utf8 letters in flag name
count := utf8.RuneCountInString(name)
if count > 2 {
count = 2 // reuse this count to generate single - or -- in flag completion
}
// if flag name has more than one utf8 letter and last argument in cli has -- prefix then
// skip flag completion for short flags example -v or -x
if strings.HasPrefix(lastArg, "--") && count == 1 {
continue
}
// match if last argument matches this flag and it is not repeated
if strings.HasPrefix(name, cur) && cur != name && !cliArgContains(name) {
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
_, _ = fmt.Fprintln(writer, flagCompletion)
}
name := strings.TrimSpace(flag.Names()[0])
// this will get total count utf8 letters in flag name
count := utf8.RuneCountInString(name)
if count > 2 {
count = 2 // reuse this count to generate single - or -- in flag completion
}
// if flag name has more than one utf8 letter and last argument in cli has -- prefix then
// skip flag completion for short flags example -v or -x
if strings.HasPrefix(lastArg, "--") && count == 1 {
continue

Check warning on line 194 in help.go

View check run for this annotation

Codecov / codecov/patch

help.go#L194

Added line #L194 was not covered by tests
}
// match if last argument matches this flag and it is not repeated
if strings.HasPrefix(name, cur) && cur != name && !cliArgContains(name) {
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
_, _ = fmt.Fprintln(writer, flagCompletion)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--happiness", "putz", "--generate-bash-completion"},
expected: "futz\nhelp\nh\n",
expected: "futz\nhelp\n",
},
{
name: "typical-subcommand-subcommand-suggestion",
Expand Down

0 comments on commit 43272e2

Please sign in to comment.