8000 Include flag descriptions when using zsh completion by Toalaah · Pull Request #1903 · urfave/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Include flag descriptions when using zsh completion #1903

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

Merged
merged 2 commits into from
May 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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