From 7339ca86997fb822cf5a010f1e3da62d3400bd02 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 5 Mar 2014 19:45:57 +0000 Subject: [PATCH] fix panic with only long flags or only one deprecatd Docker-DCO-1.1-Signed-off-by: Victor Vieux (github: vieux) --- mflag/example/example.go | 4 ++-- mflag/flag.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mflag/example/example.go b/mflag/example/example.go index 352f652..ed940e8 100644 --- a/mflag/example/example.go +++ b/mflag/example/example.go @@ -14,8 +14,8 @@ var ( func init() { flag.Bool([]string{"#hp", "#-halp"}, false, "display the halp") flag.BoolVar(&b, []string{"b"}, false, "a simple bool") - flag.BoolVar(&b2, []string{"-bool"}, false, "a simple bool") - flag.IntVar(&i, []string{"#integer", "-integer"}, -1, "a simple integer") + flag.BoolVar(&b2, []string{"#-bool"}, false, "a simple bool") + flag.IntVar(&i, []string{"-integer", "-number"}, -1, "a simple integer") flag.StringVar(&str, []string{"s", "#hidden", "-string"}, "", "a simple string") //-s -hidden and --string will work, but -hidden won't be in the usage flag.BoolVar(&h, []string{"h", "#help", "-help"}, false, "display the help") flag.Parse() diff --git a/mflag/flag.go b/mflag/flag.go index ff0de23..7125c03 100644 --- a/mflag/flag.go +++ b/mflag/flag.go @@ -290,13 +290,13 @@ type Flag struct { func sortFlags(flags map[string]*Flag) []*Flag { var list sort.StringSlice for _, f := range flags { + fName := strings.TrimPrefix(f.Names[0], "#") if len(f.Names) == 1 { - list = append(list, f.Names[0]) + list = append(list, fName) continue } found := false - fName := strings.TrimPrefix(strings.TrimPrefix(f.Names[0], "#"), "-") for _, name := range list { if name == fName { found = true