Skip to content

Commit

Permalink
Merge pull request #1740 from dearchap/issue_1737
Browse files Browse the repository at this point in the history
Fix:(issue_1737) Set bool count by taking care of num of aliases
  • Loading branch information
dearchap committed May 28, 2023
2 parents 00a9d31 + 8dfee9e commit 60247a7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
7 changes: 6 additions & 1 deletion flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *boolValue) String() string {
func (b *boolValue) IsBoolFlag() bool { return true }

func (b *boolValue) Count() int {
if b.count != nil {
if b.count != nil && *b.count > 0 {
return *b.count
}
return 0
Expand Down Expand Up @@ -130,6 +130,11 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
if count == nil {
count = new(int)
}

// since count will be incremented for each alias as well
// subtract number of aliases from overall count
*count -= len(f.Aliases)

if dest == nil {
dest = new(bool)
}
Expand Down
50 changes: 36 additions & 14 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,24 @@ func TestBoolFlagValueFromContext(t *testing.T) {
func TestBoolFlagApply_SetsCount(t *testing.T) {
v := false
count := 0
fl := BoolFlag{Name: "wat", Aliases: []string{"W", "huh"}, Destination: &v, Count: &count}
set := flag.NewFlagSet("test", 0)
err := fl.Apply(set)
expect(t, err, nil)
app := &App{
Name: "foo",
Flags: []Flag{
&BoolFlag{Name: "wat", Aliases: []string{"W", "huh"}, Destination: &v, Count: &count},
},
}

err = set.Parse([]string{"--wat", "-W", "--huh"})
err := app.Run([]string{"foo", "--wat", "-W", "--huh"})
if err == nil {
t.Error("Expected error")
} else if es := err.Error(); !strings.Contains(es, "Cannot use two forms of the same flag") {
t.Errorf("Unexpected error %s", es)
}

v = false
count = 0

err = app.Run([]string{"foo", "--wat", "--wat", "--wat"})
expect(t, err, nil)
expect(t, v, true)
expect(t, count, 3)
Expand All @@ -82,7 +94,12 @@ func TestBoolFlagCountFromContext(t *testing.T) {
expectedCount int
}{
{
input: []string{"-tf", "-w", "-huh"},
input: []string{"foo", "-tf"},
expectedVal: true,
expectedCount: 1,
},
{
input: []string{"foo", "-tf", "-tf", "-tf"},
expectedVal: true,
expectedCount: 3,
},
Expand All @@ -94,16 +111,21 @@ func TestBoolFlagCountFromContext(t *testing.T) {
}

for _, bct := range boolCountTests {
set := flag.NewFlagSet("test", 0)
ctx := NewContext(nil, set, nil)
tf := &BoolFlag{Name: "tf", Aliases: []string{"w", "huh"}}
err := tf.Apply(set)
expect(t, err, nil)
app := &App{
Name: "foo",
Flags: []Flag{tf},
Action: func(ctx *Context) error {
expect(t, tf.Get(ctx), bct.expectedVal)
expect(t, ctx.Count("tf"), bct.expectedCount)
expect(t, ctx.Count("w"), bct.expectedCount)
expect(t, ctx.Count("huh"), bct.expectedCount)
return nil
},
}

app.Run(bct.input)

err = set.Parse(bct.input)
expect(t, err, nil)
expect(t, tf.Get(ctx), bct.expectedVal)
expect(t, ctx.Count("tf"), bct.expectedCount)
}
}

Expand Down

0 comments on commit 60247a7

Please sign in to comment.