Skip to content

Commit 60247a7

Browse files
authored
Merge pull request #1740 from dearchap/issue_1737
Fix:(issue_1737) Set bool count by taking care of num of aliases
2 parents 00a9d31 + 8dfee9e commit 60247a7

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

flag_bool.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (b *boolValue) String() string {
5252
func (b *boolValue) IsBoolFlag() bool { return true }
5353

5454
func (b *boolValue) Count() int {
55-
if b.count != nil {
55+
if b.count != nil && *b.count > 0 {
5656
return *b.count
5757
}
5858
return 0
@@ -130,6 +130,11 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
130130
if count == nil {
131131
count = new(int)
132132
}
133+
134+
// since count will be incremented for each alias as well
135+
// subtract number of aliases from overall count
136+
*count -= len(f.Aliases)
137+
133138
if dest == nil {
134139
dest = new(bool)
135140
}

flag_test.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,24 @@ func TestBoolFlagValueFromContext(t *testing.T) {
6363
func TestBoolFlagApply_SetsCount(t *testing.T) {
6464
v := false
6565
count := 0
66-
fl := BoolFlag{Name: "wat", Aliases: []string{"W", "huh"}, Destination: &v, Count: &count}
67-
set := flag.NewFlagSet("test", 0)
68-
err := fl.Apply(set)
69-
expect(t, err, nil)
66+
app := &App{
67+
Name: "foo",
68+
Flags: []Flag{
69+
&BoolFlag{Name: "wat", Aliases: []string{"W", "huh"}, Destination: &v, Count: &count},
70+
},
71+
}
7072

71-
err = set.Parse([]string{"--wat", "-W", "--huh"})
73+
err := app.Run([]string{"foo", "--wat", "-W", "--huh"})
74+
if err == nil {
75+
t.Error("Expected error")
76+
} else if es := err.Error(); !strings.Contains(es, "Cannot use two forms of the same flag") {
77+
t.Errorf("Unexpected error %s", es)
78+
}
79+
80+
v = false
81+
count = 0
82+
83+
err = app.Run([]string{"foo", "--wat", "--wat", "--wat"})
7284
expect(t, err, nil)
7385
expect(t, v, true)
7486
expect(t, count, 3)
@@ -82,7 +94,12 @@ func TestBoolFlagCountFromContext(t *testing.T) {
8294
expectedCount int
8395
}{
8496
{
85-
input: []string{"-tf", "-w", "-huh"},
97+
input: []string{"foo", "-tf"},
98+
expectedVal: true,
99+
expectedCount: 1,
100+
},
101+
{
102+
input: []string{"foo", "-tf", "-tf", "-tf"},
86103
expectedVal: true,
87104
expectedCount: 3,
88105
},
@@ -94,16 +111,21 @@ func TestBoolFlagCountFromContext(t *testing.T) {
94111
}
95112

96113
for _, bct := range boolCountTests {
97-
set := flag.NewFlagSet("test", 0)
98-
ctx := NewContext(nil, set, nil)
99114
tf := &BoolFlag{Name: "tf", Aliases: []string{"w", "huh"}}
100-
err := tf.Apply(set)
101-
expect(t, err, nil)
115+
app := &App{
116+
Name: "foo",
117+
Flags: []Flag{tf},
118+
Action: func(ctx *Context) error {
119+
expect(t, tf.Get(ctx), bct.expectedVal)
120+
expect(t, ctx.Count("tf"), bct.expectedCount)
121+
expect(t, ctx.Count("w"), bct.expectedCount)
122+
expect(t, ctx.Count("huh"), bct.expectedCount)
123+
return nil
124+
},
125+
}
126+
127+
app.Run(bct.input)
102128

103-
err = set.Parse(bct.input)
104-
expect(t, err, nil)
105-
expect(t, tf.Get(ctx), bct.expectedVal)
106-
expect(t, ctx.Count("tf"), bct.expectedCount)
107129
}
108130
}
109131

0 commit comments

Comments
 (0)