diff --git a/app_test.go b/app_test.go index c68bff8ad1..19f41b707a 100644 --- a/app_test.go +++ b/app_test.go @@ -3083,6 +3083,8 @@ func TestPersistentFlag(t *testing.T) { var topInt, topPersistentInt, subCommandInt int var appFlag string + appSliceInt := NewInt64Slice() + a := &App{ Flags: []Flag{ &StringFlag{ @@ -3090,6 +3092,11 @@ func TestPersistentFlag(t *testing.T) { Persistent: true, Destination: &appFlag, }, + &Int64SliceFlag{ + Name: "persistentAppSliceFlag", + Persistent: true, + Destination: appSliceInt, + }, }, Commands: []*Command{ { @@ -3133,9 +3140,11 @@ func TestPersistentFlag(t *testing.T) { t.Errorf("Expected 'hello' got %s", appFlag) } - err = a.Run([]string{"app", "--persistentAppFlag", "hello", - "cmd", "--cmdFlag", "12", - "subcmd", "--cmdPersistentFlag", "20", "--cmdFlag", "11", "--persistentAppFlag", "bar"}) + err = a.Run([]string{"app", + "--persistentAppFlag", "hello", + "--persistentAppSliceFlag", "100", + "cmd", "--cmdFlag", "12", "--persistentAppSliceFlag", "102", + "subcmd", "--cmdPersistentFlag", "20", "--cmdFlag", "11", "--persistentAppFlag", "bar", "--persistentAppSliceFlag", "130"}) if err != nil { t.Fatal(err) @@ -3156,4 +3165,9 @@ func TestPersistentFlag(t *testing.T) { if subCommandInt != 11 { t.Errorf("Expected 11 got %d", subCommandInt) } + + expectedInt := []int64{100, 102, 130} + if !reflect.DeepEqual(appSliceInt.slice, expectedInt) { + t.Errorf("Expected %v got %d", expectedInt, appSliceInt.slice) + } } diff --git a/command.go b/command.go index cd3a8b0378..c7a8fb9e34 100644 --- a/command.go +++ b/command.go @@ -347,11 +347,11 @@ func (c *Command) parseFlags(args Args, ctx *Context) (*flag.FlagSet, error) { } } - if err = parseIter(set, c, args.Tail(), ctx.shellComplete); err != nil { + if err := parseIter(set, c, args.Tail(), ctx.shellComplete); err != nil { return nil, err } - if err = normalizeFlags(c.Flags, set); err != nil { + if err := normalizeFlags(c.Flags, set); err != nil { return nil, err } diff --git a/godoc-current.txt b/godoc-current.txt index f59fc32b9e..6b12acd3fe 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -1714,6 +1714,8 @@ func (x *SliceFlag[T, S, E]) GetUsage() string func (x *SliceFlag[T, S, E]) GetValue() string +func (x *SliceFlag[T, S, E]) IsPersistent() bool + func (x *SliceFlag[T, S, E]) IsRequired() bool func (x *SliceFlag[T, S, E]) IsSet() bool @@ -1736,6 +1738,7 @@ type SliceFlagTarget[E any] interface { DocGenerationFlag VisibleFlag CategorizableFlag + PersistentFlag // SetValue should propagate the given slice to the target, ideally as a new value. // Note that a nil slice should nil/clear any existing value (modelled as ~[]E). diff --git a/sliceflag.go b/sliceflag.go index 7dea3576a3..952d4866df 100644 --- a/sliceflag.go +++ b/sliceflag.go @@ -28,6 +28,7 @@ type ( DocGenerationFlag VisibleFlag CategorizableFlag + PersistentFlag // SetValue should propagate the given slice to the target, ideally as a new value. // Note that a nil slice should nil/clear any existing value (modelled as ~[]E). @@ -128,6 +129,7 @@ func (x *SliceFlag[T, S, E]) GetDefaultText() string { return x.Target.GetDefaul func (x *SliceFlag[T, S, E]) GetEnvVars() []string { return x.Target.GetEnvVars() } func (x *SliceFlag[T, S, E]) IsVisible() bool { return x.Target.IsVisible() } func (x *SliceFlag[T, S, E]) GetCategory() string { return x.Target.GetCategory() } +func (x *SliceFlag[T, S, E]) IsPersistent() bool { return x.Target.IsPersistent() } func (x *flagValueHook) Set(value string) error { if err := x.value.Set(value); err != nil { diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index f59fc32b9e..6b12acd3fe 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -1714,6 +1714,8 @@ func (x *SliceFlag[T, S, E]) GetUsage() string func (x *SliceFlag[T, S, E]) GetValue() string +func (x *SliceFlag[T, S, E]) IsPersistent() bool + func (x *SliceFlag[T, S, E]) IsRequired() bool func (x *SliceFlag[T, S, E]) IsSet() bool @@ -1736,6 +1738,7 @@ type SliceFlagTarget[E any] interface { DocGenerationFlag VisibleFlag CategorizableFlag + PersistentFlag // SetValue should propagate the given slice to the target, ideally as a new value. // Note that a nil slice should nil/clear any existing value (modelled as ~[]E).