From 4061f41c9aacfb1d66b941fefab8a3e900964217 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Fri, 5 May 2017 08:08:38 +0200 Subject: [PATCH] Use pflag.ShorthandLookup --- command.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/command.go b/command.go index bb26d20..4bf1e24 100644 --- a/command.go +++ b/command.go @@ -11,8 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces. -//In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code. +// Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces. +// In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code. package cobra import ( @@ -369,8 +369,8 @@ func (c *Command) HelpTemplate() string { {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}` } -func hasNoOptDefVal(name string, f *flag.FlagSet) bool { - flag := f.Lookup(name) +func hasNoOptDefVal(name string, fs *flag.FlagSet) bool { + flag := fs.Lookup(name) if flag == nil { return false } @@ -378,14 +378,15 @@ func hasNoOptDefVal(name string, f *flag.FlagSet) bool { } func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool { - result := false - fs.VisitAll(func(flag *flag.Flag) { - if flag.Shorthand == name && flag.NoOptDefVal != "" { - result = true - return - } - }) - return result + if len(name) == 0 { + return false + } + + flag := fs.ShorthandLookup(name[:1]) + if flag == nil { + return false + } + return flag.NoOptDefVal != "" } func stripFlags(args []string, c *Command) []string {