fs.Visit() returns nil flag

Signed-off-by: Sven Dowideit <SvenDowideit@docker.com>
This commit is contained in:
Sven Dowideit 2014-08-29 14:55:49 +10:00
parent 47f57a7b7c
commit 6a1cc969fc
2 changed files with 56 additions and 4 deletions

View file

@ -317,8 +317,13 @@ func (p flagSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
var list flagSlice
for _, f := range flags {
// The sorted list is based on the first name, when flag map might use the other names.
nameMap := make(map[string]string)
for n, f := range flags {
fName := strings.TrimPrefix(f.Names[0], "#")
nameMap[fName] = n
if len(f.Names) == 1 {
list = append(list, fName)
continue
@ -338,7 +343,7 @@ func sortFlags(flags map[string]*Flag) []*Flag {
sort.Sort(list)
result := make([]*Flag, len(list))
for i, name := range list {
result[i] = flags[name]
result[i] = flags[nameMap[name]]
}
return result
}
@ -473,7 +478,7 @@ var Usage = func() {
}
// FlagCount returns the number of flags that have been defined.
func (f *FlagSet) FlagCount() int { return len(f.formal) }
func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) }
// FlagCountUndeprecated returns the number of undeprecated flags that have been defined.
func (f *FlagSet) FlagCountUndeprecated() int {