Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions flag_bool_with_inverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,14 @@ func (bif *BoolWithInverseFlag) inversePrefix() string {
}

func (bif *BoolWithInverseFlag) PreParse() error {
count := bif.Config.Count

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear why this needs to be removed. In the absence of user providing a count pointer we use one internally

if count == nil {
count = &bif.count
}
dest := bif.Destination
if dest == nil {
dest = new(bool)
}
*dest = bif.Value
bif.value = &boolValue{
destination: dest,
count: count,
count: bif.Config.Count,
}

// Validate the given default or values set from external sources as well
Expand Down Expand Up @@ -153,6 +149,7 @@ func (bif *BoolWithInverseFlag) Set(name, val string) error {
}
bif.nset = true
}
bif.count++

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to increase count irrespective of whether the flag is set or not?


if bif.Validator != nil {
return bif.Validator(bif.value.Get().(bool))
Expand Down
31 changes: 31 additions & 0 deletions flag_bool_with_inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"fmt"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -572,3 +573,33 @@ func TestBoolWithInverseFlagStringNoPanicWithNoTabStringer(t *testing.T) {
t.Errorf("expected String() to contain the flag name, got %q", got)
}
}

func TestBoolWithInverseFlagCount(t *testing.T) {
for _, externalCount := range []bool{false, true} {
for _, onlyOnce := range []bool{false, true} {
for _, name := range []string{"--env", "--no-env", "-e", "--no-e"} {
t.Run(fmt.Sprintf("external=%v/onlyOnce=%v/%s", externalCount, onlyOnce, name), func(t *testing.T) {
const initialCount = 7
count := initialCount
fl := &BoolWithInverseFlag{Name: "env", Aliases: []string{"e"}, OnlyOnce: onlyOnce}
if externalCount {
fl.Config.Count = &count
}
cmd := &Command{Flags: []Flag{fl}, Writer: io.Discard, ErrWriter: io.Discard}
err := cmd.Run(buildTestContext(t), []string{"prog", name, name})
wantCount := 2
if onlyOnce {
require.ErrorContains(t, err, "can't duplicate this flag")
wantCount = 1
} else {
require.NoError(t, err)
}
require.Equal(t, wantCount, fl.Count())
if externalCount {
require.Equal(t, initialCount+wantCount, count)
}
})
}
}
}
}