Skip to content

Commit

Permalink
Fix:(issue_1850) Add RunAction for uint/uint64 slice flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Dec 25, 2023
1 parent c667cc0 commit c5d5b78
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 },
}
Expand Down Expand Up @@ -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"},
Expand Down
9 changes: 9 additions & 0 deletions flag_uint64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions flag_uint_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c5d5b78

Please sign in to comment.