Skip to content

Commit

Permalink
Merge pull request #1637 from urfave/revert-1636-issue_1071
Browse files Browse the repository at this point in the history
Revert "Feature:(issue_1071) Add check for duplicated flags"
  • Loading branch information
dearchap authored Jan 9, 2023
2 parents f60d3ef + 1451fbf commit 6686660
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 102 deletions.
61 changes: 0 additions & 61 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3234,64 +3234,3 @@ 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)
}
})
}
}
38 changes: 1 addition & 37 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,6 @@ 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 {
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
}

// ValueCreator is responsible for creating a flag.Value emulation
// as well as custom formatting
//
Expand Down Expand Up @@ -86,8 +57,6 @@ 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 @@ -139,13 +108,8 @@ 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(vw, name, f.Usage)
set.Var(f.value, name, f.Usage)
}

f.applied = true
Expand Down
2 changes: 0 additions & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,6 @@ 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: 0 additions & 2 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,6 @@ 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

0 comments on commit 6686660

Please sign in to comment.