Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1071) Add support for duplicated flag check #1638

Merged
merged 4 commits into from
Jan 9, 2023
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
61 changes: 61 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3234,3 +3234,64 @@ func TestPersistentFlag(t *testing.T) {
}

}

func TestFlagDuplicates(t *testing.T) {

a := &App{
Flags: []Flag{
&StringFlag{
Name: "sflag",
OnlyOnce: true,
},
&Int64SliceFlag{
Name: "isflag",
},
&Float64SliceFlag{
Name: "fsflag",
OnlyOnce: true,
},
&IntFlag{
Name: "iflag",
},
},
Action: func(ctx *Context) error {
return nil
},
}

tests := []struct {
name string
args []string
errExpected bool
}{
{
name: "all args present once",
args: []string{"foo", "--sflag", "hello", "--isflag", "1", "--isflag", "2", "--fsflag", "2.0", "--iflag", "10"},
},
{
name: "duplicate non slice flag(duplicatable)",
args: []string{"foo", "--sflag", "hello", "--isflag", "1", "--isflag", "2", "--fsflag", "2.0", "--iflag", "10", "--iflag", "20"},
},
{
name: "duplicate non slice flag(non duplicatable)",
args: []string{"foo", "--sflag", "hello", "--isflag", "1", "--isflag", "2", "--fsflag", "2.0", "--iflag", "10", "--sflag", "trip"},
errExpected: true,
},
{
name: "duplicate slice flag(non duplicatable)",
args: []string{"foo", "--sflag", "hello", "--isflag", "1", "--isflag", "2", "--fsflag", "2.0", "--fsflag", "3.0", "--iflag", "10"},
errExpected: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := a.Run(test.args)
if test.errExpected && err == nil {
t.Error("expected error")
} else if !test.errExpected && err != nil {
t.Error(err)
}
})
}
}
4 changes: 2 additions & 2 deletions flag_float64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (cCtx *Context) Float64Slice(name string) []float64 {
func lookupFloat64Slice(name string, set *flag.FlagSet) []float64 {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*Float64Slice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]float64); ok {
return slice
}
}
return nil
Expand Down
55 changes: 54 additions & 1 deletion flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,52 @@ type Value interface {
flag.Getter
}

// simple wrapper to intercept Value operations
// to check for duplicates
type valueWrapper struct {
value Value
count int
onlyOnce bool
}

func (v *valueWrapper) String() string {
if v.value == nil {
return ""
}
return v.value.String()
}

func (v *valueWrapper) Set(s string) error {
if v.count == 1 && v.onlyOnce {
return fmt.Errorf("cant duplicate this flag")
}
v.count++
return v.value.Set(s)
}

func (v *valueWrapper) Get() any {
return v.value.Get()
}

func (v *valueWrapper) IsBoolFlag() bool {
_, ok := v.value.(*boolValue)
return ok
}

func (v *valueWrapper) Serialize() string {
if s, ok := v.value.(Serializer); ok {
return s.Serialize()
}
return v.value.String()
}

func (v *valueWrapper) Count() int {
if s, ok := v.value.(Countable); ok {
return s.Count()
}
return 0
}

// ValueCreator is responsible for creating a flag.Value emulation
// as well as custom formatting
//
Expand Down Expand Up @@ -57,6 +103,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {

Config C // Additional/Custom configuration associated with this flag type

OnlyOnce bool // whether this flag can be duplicated on the command line

// unexported fields for internal use
hasBeenSet bool // whether the flag has been set from env or file
applied bool // whether the flag has been applied to a flag set already
Expand Down Expand Up @@ -108,8 +156,13 @@ func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
}
}

vw := &valueWrapper{
value: f.value,
onlyOnce: f.OnlyOnce,
}

for _, name := range f.Names() {
set.Var(f.value, name, f.Usage)
set.Var(vw, name, f.Usage)
}

f.applied = true
Expand Down
4 changes: 2 additions & 2 deletions flag_int64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (cCtx *Context) Int64Slice(name string) []int64 {
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*Int64Slice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]int64); ok {
return slice
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions flag_int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (cCtx *Context) IntSlice(name string) []int {
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*IntSlice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]int); ok {
return slice
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions flag_string_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (cCtx *Context) StringMap(name string) map[string]string {
func lookupStringMap(name string, set *flag.FlagSet) map[string]string {
f := set.Lookup(name)
if f != nil {
if mapping, ok := f.Value.(*StringMap); ok {
return mapping.Value()
if mapping, ok := f.Value.(flag.Getter).Get().(map[string]string); ok {
return mapping
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions flag_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (cCtx *Context) StringSlice(name string) []string {
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*StringSlice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]string); ok {
return slice
}
}
return nil
Expand Down
34 changes: 17 additions & 17 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func TestStringSliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*StringSlice).Value(), []string{"vincent van goat", "scape goat"})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), []string{"vincent van goat", "scape goat"})
}

func TestStringSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -615,7 +615,7 @@ func TestStringSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
_ = fl.Apply(set)
err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*StringSlice).Value(), []string{"vincent van goat", "scape goat"})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), []string{"vincent van goat", "scape goat"})
}

func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -959,7 +959,7 @@ func TestIntSliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*IntSlice).Value(), []int{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), []int{1, 2})
}

func TestIntSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -973,7 +973,7 @@ func TestIntSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val, []int{3, 4})
expect(t, set.Lookup("goat").Value.(*IntSlice).Value(), []int{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), []int{1, 2})
}

func TestIntSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -1098,7 +1098,7 @@ func TestInt64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*Int64Slice).Value(), []int64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), []int64{1, 2})
}

func TestInt64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -1112,7 +1112,7 @@ func TestInt64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val.Value(), []int64{3, 4})
expect(t, set.Lookup("goat").Value.(*Int64Slice).Value(), []int64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]int64), []int64{1, 2})
}

func TestInt64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -1254,7 +1254,7 @@ func TestUintSliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*UintSlice).Value(), []uint{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]uint), []uint{1, 2})
}

func TestUintSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -1268,7 +1268,7 @@ func TestUintSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val.Value(), []uint{3, 4})
expect(t, set.Lookup("goat").Value.(*UintSlice).Value(), []uint{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]uint), []uint{1, 2})
}

func TestUintSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -1401,7 +1401,7 @@ func TestUint64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*Uint64Slice).Value(), []uint64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]uint64), []uint64{1, 2})
}

func TestUint64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -1414,7 +1414,7 @@ func TestUint64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
_ = fl.Apply(set)
err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*Uint64Slice).Value(), []uint64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]uint64), []uint64{1, 2})
}

func TestUint64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -1601,7 +1601,7 @@ func TestFloat64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {

err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*Float64Slice).Value(), []float64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]float64), []float64{1, 2})
}

func TestFloat64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -1614,7 +1614,7 @@ func TestFloat64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
_ = fl.Apply(set)
err := set.Parse(nil)
expect(t, err, nil)
expect(t, set.Lookup("goat").Value.(*Float64Slice).Value(), []float64{1, 2})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get().([]float64), []float64{1, 2})
}

func TestFloat64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down Expand Up @@ -2708,7 +2708,7 @@ func TestTimestampFlagApply(t *testing.T) {

err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})
expect(t, err, nil)
expect(t, *set.Lookup("time").Value.(*timestampValue).timestamp, expectedResult)
expect(t, set.Lookup("time").Value.(flag.Getter).Get(), expectedResult)
}

func TestTimestampFlagApplyValue(t *testing.T) {
Expand All @@ -2719,7 +2719,7 @@ func TestTimestampFlagApplyValue(t *testing.T) {

err := set.Parse([]string{""})
expect(t, err, nil)
expect(t, *set.Lookup("time").Value.(*timestampValue).timestamp, expectedResult)
expect(t, set.Lookup("time").Value.(flag.Getter).Get(), expectedResult)
}

func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
Expand Down Expand Up @@ -2751,7 +2751,7 @@ func TestTimestampFlagApply_Timezoned(t *testing.T) {

err := set.Parse([]string{"--time", "Mon Jan 2 08:04:05 2006"})
expect(t, err, nil)
expect(t, *set.Lookup("time").Value.(*timestampValue).timestamp, expectedResult.In(pdt))
expect(t, set.Lookup("time").Value.(flag.Getter).Get(), expectedResult.In(pdt))
}

func TestTimestampFlagValueFromContext(t *testing.T) {
Expand Down Expand Up @@ -3230,7 +3230,7 @@ func TestStringMapFlagApply_UsesEnvValues_noDefault(t *testing.T) {
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val, map[string]string(nil))
expect(t, set.Lookup("goat").Value.(*StringMap).Value(), map[string]string{"vincent van goat": "scape goat"})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), map[string]string{"vincent van goat": "scape goat"})
}

func TestStringMapFlagApply_UsesEnvValues_withDefault(t *testing.T) {
Expand All @@ -3244,7 +3244,7 @@ func TestStringMapFlagApply_UsesEnvValues_withDefault(t *testing.T) {
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val, map[string]string{`some default`: `values here`})
expect(t, set.Lookup("goat").Value.(*StringMap).Value(), map[string]string{"vincent van goat": "scape goat"})
expect(t, set.Lookup("goat").Value.(flag.Getter).Get(), map[string]string{"vincent van goat": "scape goat"})
}

func TestStringMapFlagApply_DefaultValueWithDestination(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions flag_uint64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (cCtx *Context) Uint64Slice(name string) []uint64 {
func lookupUint64Slice(name string, set *flag.FlagSet) []uint64 {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*Uint64Slice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]uint64); ok {
return slice
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions flag_uint_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (cCtx *Context) UintSlice(name string) []uint {
func lookupUintSlice(name string, set *flag.FlagSet) []uint {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(*UintSlice); ok {
return slice.Value()
if slice, ok := f.Value.(flag.Getter).Get().([]uint); ok {
return slice
}
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {

Config C // Additional/Custom configuration associated with this flag type

OnlyOnce bool // whether this flag can be duplicated on the command line

// Has unexported fields.
}
FlagBase[T,C,VC] is a generic flag base which can be used as a boilerplate
Expand Down
2 changes: 2 additions & 0 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {

Config C // Additional/Custom configuration associated with this flag type

OnlyOnce bool // whether this flag can be duplicated on the command line

// Has unexported fields.
}
FlagBase[T,C,VC] is a generic flag base which can be used as a boilerplate
Expand Down