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

Revert "Feature:(issue_1071) Add check for duplicated flags" #1637

Merged
merged 1 commit 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: 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