Skip to content

Commit

Permalink
Add persistence for slice flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Nov 7, 2022
1 parent 66bbfac commit 2d8e012
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
20 changes: 17 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3083,13 +3083,20 @@ func TestPersistentFlag(t *testing.T) {
var topInt, topPersistentInt, subCommandInt int
var appFlag string

appSliceInt := NewInt64Slice()

a := &App{
Flags: []Flag{
&StringFlag{
Name: "persistentAppFlag",
Persistent: true,
Destination: &appFlag,
},
&Int64SliceFlag{
Name: "persistentAppSliceFlag",
Persistent: true,
Destination: appSliceInt,
},
},
Commands: []*Command{
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions sliceflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down

0 comments on commit 2d8e012

Please sign in to comment.