From 009ebb14c97e823e3e541222b1c58a738b70b975 Mon Sep 17 00:00:00 2001 From: Brandon Roehl Date: Wed, 27 Jun 2018 11:10:57 -0500 Subject: [PATCH] All test passing --- zsh_completions.go | 21 ++++++++++++++++----- zsh_completions_test.go | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/zsh_completions.go b/zsh_completions.go index f1f1709c..d254cfd3 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -1,4 +1,4 @@ -package main +package cobra import ( "bytes" @@ -6,6 +6,8 @@ import ( "io" "os" "strings" + + flag "github.com/spf13/pflag" ) // GenZshCompletionFile generates zsh completion file. @@ -124,7 +126,11 @@ func commandNames(command *Command) []string { commands := command.Commands() ns := make([]string, len(commands)) for i, c := range commands { - ns[i] = fmt.Sprintf("%s[%s]", c.Name(), c.Short) + commandMsg := c.Name() + if len(c.Short) > 0 { + commandMsg += fmt.Sprintf("[%s]", c.Short) + } + ns[i] = commandMsg } return ns } @@ -132,12 +138,17 @@ func commandNames(command *Command) []string { func commandFlags(command *Command) []string { flags := command.Flags() ns := make([]string, 0) - flags.VisitAll(func(flag *pflag.Flag) { + flags.VisitAll(func(flag *flag.Flag) { + var flagMsg string if len(flag.Shorthand) > 0 { - ns = append(ns, fmt.Sprintf("{-%s,--%s}'[%s]'", flag.Shorthand, flag.Name, flag.Usage)) + flagMsg = fmt.Sprintf("{-%s,--%s}", flag.Shorthand, flag.Name) } else { - ns = append(ns, fmt.Sprintf("--%s'[%s]'", flag.Name, flag.Usage)) + flagMsg = fmt.Sprintf("--%s", flag.Name) } + if len(flag.Usage) > 0 { + flagMsg += fmt.Sprintf("'[%s]'", flag.Usage) + } + ns = append(ns, flagMsg) }) return ns } diff --git a/zsh_completions_test.go b/zsh_completions_test.go index 34e69496..f8d5388a 100644 --- a/zsh_completions_test.go +++ b/zsh_completions_test.go @@ -42,7 +42,7 @@ func TestZshCompletion(t *testing.T) { r.AddCommand(&Command{Use: "c2"}) return r }(), - expectedExpressions: []string{"(c1 c2)"}, + expectedExpressions: []string{"'c1' 'c2'"}, }, { name: "tree", @@ -69,7 +69,7 @@ func TestZshCompletion(t *testing.T) { return r }(), - expectedExpressions: []string{"(sub11 sub12)", "(sub21 sub22)"}, + expectedExpressions: []string{"'sub11' 'sub12'", "'sub21' 'sub22'"}, }, }