From 8dfee9e74789c450660f59db3ce38382c9a012cc Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sat, 27 May 2023 19:46:26 -0400 Subject: [PATCH] Fix:(issue_1737) Set bool count by taking care of num of aliases --- flag_bool.go | 7 ++++++- flag_test.go | 50 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/flag_bool.go b/flag_bool.go index f64d1cd92c..369d18b77f 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -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 @@ -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) } diff --git a/flag_test.go b/flag_test.go index 21ded7545c..465750df78 100644 --- a/flag_test.go +++ b/flag_test.go @@ -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) @@ -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, }, @@ -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) } }