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
11 changes: 10 additions & 1 deletion flag_bool_with_inverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (bif *BoolWithInverseFlag) IsSet() bool {
}

func (bif *BoolWithInverseFlag) Get() any {
return bif.value.Get()
if bif.value != nil {
return bif.value.Get()
}
return bif.Value
}

func (bif *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command) error {
Expand Down Expand Up @@ -136,6 +139,12 @@ func (bif *BoolWithInverseFlag) PostParse() error {
}

func (bif *BoolWithInverseFlag) Set(name, val string) error {
if !bif.applied {
if err := bif.PreParse(); err != nil {
return err
}
}

// A value from Sources is only a fallback, so a value set afterwards
// replaces it (see FlagBase.Set).
if bif.fromSource {
Expand Down
56 changes: 56 additions & 0 deletions flag_bool_with_inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,62 @@ import (

var errBothEnvFlagsAreSet = fmt.Errorf("cannot set both flags `--env` and `--no-env`")

func TestBoolWithInverseGetBeforeParse(t *testing.T) {
for _, value := range []bool{false, true} {
t.Run(fmt.Sprint(value), func(t *testing.T) {
destination := !value
flag := &BoolWithInverseFlag{Name: "env", Value: value, Destination: &destination}
cmd := &Command{Flags: []Flag{flag}}

require.Equal(t, value, flag.Get())
require.Equal(t, value, cmd.Bool("env"))
require.False(t, flag.IsSet())
require.Zero(t, flag.Count())
require.Equal(t, !value, destination)
})
}
}

func TestBoolWithInverseSetBeforeParseValidatesDefault(t *testing.T) {
wantErr := fmt.Errorf("invalid default")
flag := &BoolWithInverseFlag{
Name: "env",
ValidateDefaults: true,
Validator: func(bool) error { return wantErr },
}
cmd := &Command{Flags: []Flag{flag}}

require.ErrorIs(t, cmd.Set("env", "true"), wantErr)
require.False(t, flag.IsSet())
require.Zero(t, flag.Count())
}

func TestBoolWithInverseSetBeforeParse(t *testing.T) {
for _, tc := range []struct {
name string
value string
want bool
}{
{name: "env", value: "true", want: true},
{name: "env", value: "false", want: false},
{name: "e", value: "true", want: true},
{name: "no-env", value: "true", want: false},
{name: "no-env", value: "false", want: true},
} {
t.Run(tc.name+"="+tc.value, func(t *testing.T) {
var destination bool
flag := &BoolWithInverseFlag{Name: "env", Aliases: []string{"e"}, Destination: &destination}
cmd := &Command{Flags: []Flag{flag}}

require.NoError(t, cmd.Set(tc.name, tc.value))
require.Equal(t, tc.want, cmd.Bool("env"))
require.Equal(t, tc.want, destination)
require.True(t, flag.IsSet())
require.Equal(t, 1, flag.Count())
})
}
}

type boolWithInverseTestCase struct {
args []string
toBeSet bool
Expand Down
Loading