From c5d5b78732a020e5b51750ba090781f10a0ee0e7 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Mon, 25 Dec 2023 04:03:37 -0500 Subject: [PATCH] Fix:(issue_1850) Add RunAction for uint/uint64 slice flags --- app_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ flag_uint64_slice.go | 9 +++++++++ flag_uint_slice.go | 9 +++++++++ godoc-current.txt | 6 ++++++ testdata/godoc-v2.x.txt | 6 ++++++ 5 files changed, 70 insertions(+) diff --git a/app_test.go b/app_test.go index c7bc9171e8..cf9680f541 100644 --- a/app_test.go +++ b/app_test.go @@ -2888,6 +2888,16 @@ func TestFlagAction(t *testing.T) { return err }, }, + &UintSliceFlag{ + Name: "f_uint_slice", + Action: func(c *Context, v []uint) error { + if len(v) > 0 && v[0] == 0 { + return fmt.Errorf("invalid uint slice") + } + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err + }, + }, &Uint64Flag{ Name: "f_uint64", Action: func(c *Context, v uint64) error { @@ -2898,6 +2908,16 @@ func TestFlagAction(t *testing.T) { return err }, }, + &Uint64SliceFlag{ + Name: "f_uint64_slice", + Action: func(c *Context, v []uint64) error { + if len(v) > 0 && v[0] == 0 { + return fmt.Errorf("invalid uint64 slice") + } + _, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v))) + return err + }, + }, }, Action: func(ctx *Context) error { return nil }, } @@ -3048,11 +3068,31 @@ func TestFlagAction(t *testing.T) { args: []string{"app", "--f_uint=0"}, err: fmt.Errorf("zero uint"), }, + { + name: "flag_uint_slice", + args: []string{"app", "--f_uint_slice=1,2,3"}, + exp: "[1 2 3] ", + }, + { + name: "flag_uint_slice", + args: []string{"app", "--f_uint_slice=0"}, + err: fmt.Errorf("invalid uint slice"), + }, { name: "flag_uint64", args: []string{"app", "--f_uint64=1"}, exp: "1 ", }, + { + name: "flag_uint64_slice", + args: []string{"app", "--f_uint64_slice=1,2,3"}, + exp: "[1 2 3] ", + }, + { + name: "flag_uint64_slice", + args: []string{"app", "--f_uint64_slice=0"}, + err: fmt.Errorf("invalid uint64 slice"), + }, { name: "flag_uint64_error", args: []string{"app", "--f_uint64=0"}, diff --git a/flag_uint64_slice.go b/flag_uint64_slice.go index e845dd5257..d342018686 100644 --- a/flag_uint64_slice.go +++ b/flag_uint64_slice.go @@ -190,6 +190,15 @@ func (f *Uint64SliceFlag) Get(ctx *Context) []uint64 { return ctx.Uint64Slice(f.Name) } +// RunAction executes flag action if set +func (f *Uint64SliceFlag) RunAction(c *Context) error { + if f.Action != nil { + return f.Action(c, c.Uint64Slice(f.Name)) + } + + return nil +} + // Uint64Slice looks up the value of a local Uint64SliceFlag, returns // nil if not found func (cCtx *Context) Uint64Slice(name string) []uint64 { diff --git a/flag_uint_slice.go b/flag_uint_slice.go index d2aed480d9..4dc13e126a 100644 --- a/flag_uint_slice.go +++ b/flag_uint_slice.go @@ -201,6 +201,15 @@ func (f *UintSliceFlag) Get(ctx *Context) []uint { return ctx.UintSlice(f.Name) } +// RunAction executes flag action if set +func (f *UintSliceFlag) RunAction(c *Context) error { + if f.Action != nil { + return f.Action(c, c.UintSlice(f.Name)) + } + + return nil +} + // UintSlice looks up the value of a local UintSliceFlag, returns // nil if not found func (cCtx *Context) UintSlice(name string) []uint { diff --git a/godoc-current.txt b/godoc-current.txt index 290d3fe3a6..e87b13a66c 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -2146,6 +2146,9 @@ func (f *Uint64SliceFlag) IsVisible() bool func (f *Uint64SliceFlag) Names() []string Names returns the names of the flag +func (f *Uint64SliceFlag) RunAction(c *Context) error + RunAction executes flag action if set + func (f *Uint64SliceFlag) String() string String returns a readable representation of this value (for usage defaults) @@ -2311,6 +2314,9 @@ func (f *UintSliceFlag) IsVisible() bool func (f *UintSliceFlag) Names() []string Names returns the names of the flag +func (f *UintSliceFlag) RunAction(c *Context) error + RunAction executes flag action if set + func (f *UintSliceFlag) String() string String returns a readable representation of this value (for usage defaults) diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index 290d3fe3a6..e87b13a66c 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -2146,6 +2146,9 @@ func (f *Uint64SliceFlag) IsVisible() bool func (f *Uint64SliceFlag) Names() []string Names returns the names of the flag +func (f *Uint64SliceFlag) RunAction(c *Context) error + RunAction executes flag action if set + func (f *Uint64SliceFlag) String() string String returns a readable representation of this value (for usage defaults) @@ -2311,6 +2314,9 @@ func (f *UintSliceFlag) IsVisible() bool func (f *UintSliceFlag) Names() []string Names returns the names of the flag +func (f *UintSliceFlag) RunAction(c *Context) error + RunAction executes flag action if set + func (f *UintSliceFlag) String() string String returns a readable representation of this value (for usage defaults)