Repository: urfave/cli
Pull request #1637 (2023-01-09 12:53:00)

Revert "Feature:(issue_1071) Add check for duplicated flags"
Reverts urfave/cli#1636

Tests are failing


Patch:
diff --git a/flag_impl.go b/flag_impl.go
index 2811445..855e40e 100644
--- a/flag_impl.go
+++ b/flag_impl.go
@@ -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
 //
@@ -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
@@ -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
diff --git a/godoc-current.txt b/godoc-current.txt
index 1aebcd6..9d7be18 100644
--- a/godoc-current.txt
+++ b/godoc-current.txt
@@ -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


Test patch:
diff --git a/app_test.go b/app_test.go
index ba423d5..00e980d 100644
--- a/app_test.go
+++ b/app_test.go
@@ -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)
-			}
-		})
-	}
-}
diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt
index 1aebcd6..9d7be18 100644
--- a/testdata/godoc-v3.x.txt
+++ b/testdata/godoc-v3.x.txt
@@ -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