Skip to content

Commit

Permalink
Merge pull request #1882 from bartekpacia/v3/fix/completion_aliases
Browse files Browse the repository at this point in the history
Stop command aliases and flag aliases from being suggested in shell completion
  • Loading branch information
meatballhat committed Apr 29, 2024
2 parents 6bba9e8 + 6e38501 commit 8996374
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/v3/examples/bash-completions.md
Expand Up @@ -188,7 +188,7 @@ The default shell completion flag (`--generate-bash-completion`) is defined as

<!-- {
"args": ["&#45;&#45;generate&#45;shell&#45;completion"],
"output": "wat\nhelp\nh"
"output": "wat\nhelp\n"
} -->
```go
package main
Expand Down
7 changes: 0 additions & 7 deletions examples_test.go
Expand Up @@ -269,11 +269,8 @@ func ExampleCommand_Run_shellComplete_bash_withShortFlag() {
_ = cmd.Run(context.Background(), os.Args)
// Output:
// --other
// -o
// --xyz
// -x
// --help
// -h
}

func ExampleCommand_Run_shellComplete_bash_withLongFlag() {
Expand Down Expand Up @@ -376,10 +373,8 @@ func ExampleCommand_Run_shellComplete_bash() {
_ = cmd.Run(context.Background(), os.Args)
// Output:
// describeit
// d
// next
// help
// h
}

func ExampleCommand_Run_shellComplete_zsh() {
Expand Down Expand Up @@ -415,10 +410,8 @@ func ExampleCommand_Run_shellComplete_zsh() {
_ = cmd.Run(context.Background(), 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 ExampleCommand_Run_sliceValues() {
Expand Down
42 changes: 19 additions & 23 deletions help.go
Expand Up @@ -160,13 +160,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 @@ -195,23 +191,23 @@ 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
}
// 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

0 comments on commit 8996374

Please sign in to comment.