Skip to content

Commit

Permalink
Merge pull request #1903 from Toalaah/main
Browse files Browse the repository at this point in the history
Include flag descriptions when using zsh completion
  • Loading branch information
bartekpacia committed May 2, 2024
2 parents 043b774 + fa5316e commit c44118c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
9 changes: 8 additions & 1 deletion help.go
Expand Up @@ -192,6 +192,11 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
continue
}

usage := ""
if docFlag, ok := flag.(DocGenerationFlag); ok {
usage = docFlag.GetUsage()
}

name := strings.TrimSpace(flag.Names()[0])
// this will get total count utf8 letters in flag name
count := utf8.RuneCountInString(name)
Expand All @@ -206,8 +211,10 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
// 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)
if usage != "" && strings.HasSuffix(os.Getenv("SHELL"), "zsh") {
flagCompletion = fmt.Sprintf("%s:%s", flagCompletion, usage)
}
fmt.Fprintln(writer, flagCompletion)

}
}
}
Expand Down
50 changes: 48 additions & 2 deletions help_test.go
Expand Up @@ -1080,18 +1080,18 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
origArgv := os.Args
t.Cleanup(func() { os.Args = origArgv })

t.Setenv("SHELL", "bash")

for _, tc := range []struct {
name string
cmd *Command
argv []string
env map[string]string
expected string
}{
{
name: "empty",
cmd: &Command{},
argv: []string{"prog", "cmd"},
env: map[string]string{"SHELL": "bash"},
expected: "",
},
{
Expand All @@ -1113,6 +1113,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "--excitement\n",
},
{
Expand All @@ -1135,6 +1136,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "futz\n",
},
{
Expand All @@ -1157,15 +1159,59 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--url", "http://localhost:8000", "h", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "help\n",
},
{
name: "zsh-autocomplete-with-flag-descriptions",
cmd: &Command{
Name: "putz",
Flags: []Flag{
&BoolFlag{Name: "excitement", Usage: "an exciting flag"},
&StringFlag{Name: "hat-shape"},
},
parent: &Command{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&IntFlag{Name: "everybody-jump-on"},
},
},
},
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "zsh"},
expected: "--excitement:an exciting flag\n",
},
{
name: "zsh-autocomplete-with-empty-flag-descriptions",
cmd: &Command{
Name: "putz",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
parent: &Command{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&IntFlag{Name: "everybody-jump-on"},
},
},
},
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "zsh"},
expected: "--excitement\n",
},
} {
t.Run(tc.name, func(ct *testing.T) {
writer := &bytes.Buffer{}
rootCmd := tc.cmd.Root()
rootCmd.Writer = writer

os.Args = tc.argv
for k, v := range tc.env {
t.Setenv(k, v)
}
f := DefaultCompleteWithFlags(tc.cmd)
f(context.Background(), tc.cmd)

Expand Down

0 comments on commit c44118c

Please sign in to comment.