From 117c4901623fdb1660f9816665cafa76c3c3ff5c Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 4 Apr 2024 00:40:19 +0200 Subject: [PATCH 1/2] shell completion: do not suggest aliases --- help.go | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/help.go b/help.go index d4a056adc6..8f9cb5bd98 100644 --- a/help.go +++ b/help.go @@ -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) } } } @@ -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) + } } } From 6e38501c9872cd13967341aa67e7eb42c0b64c0e Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 4 Apr 2024 00:43:10 +0200 Subject: [PATCH 2/2] fix tests --- docs/v3/examples/bash-completions.md | 2 +- examples_test.go | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/docs/v3/examples/bash-completions.md b/docs/v3/examples/bash-completions.md index 73c7fde707..737401ca63 100644 --- a/docs/v3/examples/bash-completions.md +++ b/docs/v3/examples/bash-completions.md @@ -188,7 +188,7 @@ The default shell completion flag (`--generate-bash-completion`) is defined as ```go package main diff --git a/examples_test.go b/examples_test.go index 1d0ee0d25f..b7050b801d 100644 --- a/examples_test.go +++ b/examples_test.go @@ -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() { @@ -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() { @@ -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() {