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

Stop command aliases and flag aliases from being suggested in shell completion #1882

Merged
merged 2 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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 @@
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 204 in help.go

View check run for this annotation

Codecov / codecov/patch

help.go#L204

Added line #L204 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