Skip to content

Commit d0309f1

Browse files
committed
Add flagsNyName back
1 parent be9fe83 commit d0309f1

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

flag.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ var FlagEnvHinter FlagEnvHintFunc = withEnvHint
7373
// details. This is used by the default FlagStringer.
7474
var FlagFileHinter FlagFileHintFunc = withFileHint
7575

76+
// FlagsByName is a slice of Flag.
77+
type FlagsByName []Flag
78+
79+
func (f FlagsByName) Len() int {
80+
return len(f)
81+
}
82+
83+
func (f FlagsByName) Less(i, j int) bool {
84+
if len(f[j].Names()) == 0 {
85+
return false
86+
} else if len(f[i].Names()) == 0 {
87+
return true
88+
}
89+
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
90+
}
91+
92+
func (f FlagsByName) Swap(i, j int) {
93+
f[i], f[j] = f[j], f[i]
94+
}
95+
7696
// ActionableFlag is an interface that wraps Flag interface and RunAction operation.
7797
type ActionableFlag interface {
7898
RunAction(context.Context, *Command) error

flag_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"reflect"
1111
"regexp"
12+
"sort"
1213
"strings"
1314
"testing"
1415
"time"
@@ -3049,3 +3050,31 @@ func TestFileHint(t *testing.T) {
30493050
assert.Equal(t, "foo", withFileHint("", "foo"))
30503051
assert.Equal(t, "bar [/tmp/foo.txt]", withFileHint("/tmp/foo.txt", "bar"))
30513052
}
3053+
3054+
func TestFlagsByName(t *testing.T) {
3055+
flags := []Flag{
3056+
&StringFlag{
3057+
Name: "b2",
3058+
},
3059+
&IntFlag{
3060+
Name: "a0",
3061+
},
3062+
&FloatFlag{
3063+
Name: "b1",
3064+
},
3065+
}
3066+
3067+
flagsByName := FlagsByName(flags)
3068+
sort.Sort(flagsByName)
3069+
3070+
assert.Equal(t, len(flags), flagsByName.Len())
3071+
3072+
var prev Flag
3073+
for _, f := range flags {
3074+
if prev == nil {
3075+
prev = f
3076+
} else {
3077+
assert.LessOrEqual(t, prev.Names()[0], f.Names()[0])
3078+
}
3079+
}
3080+
}

godoc-current.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,15 @@ var FlagStringer FlagStringFunc = stringifyFlag
761761
FlagStringer converts a flag definition to a string. This is used by help to
762762
display a flag.
763763

764+
type FlagsByName []Flag
765+
FlagsByName is a slice of Flag.
766+
767+
func (f FlagsByName) Len() int
768+
769+
func (f FlagsByName) Less(i, j int) bool
770+
771+
func (f FlagsByName) Swap(i, j int)
772+
764773
type FloatArg = ArgumentBase[float64, NoConfig, floatValue]
765774

766775
type FloatFlag = FlagBase[float64, NoConfig, floatValue]

testdata/godoc-v3.x.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,15 @@ var FlagStringer FlagStringFunc = stringifyFlag
761761
FlagStringer converts a flag definition to a string. This is used by help to
762762
display a flag.
763763

764+
type FlagsByName []Flag
765+
FlagsByName is a slice of Flag.
766+
767+
func (f FlagsByName) Len() int
768+
769+
func (f FlagsByName) Less(i, j int) bool
770+
771+
func (f FlagsByName) Swap(i, j int)
772+
764773
type FloatArg = ArgumentBase[float64, NoConfig, floatValue]
765774

766775
type FloatFlag = FlagBase[float64, NoConfig, floatValue]

0 commit comments

Comments
 (0)