Skip to content
Open
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
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++

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)
}
})
}
}
}
}