Skip to content

Commit c5d5b78

Browse files
committed
Fix:(issue_1850) Add RunAction for uint/uint64 slice flags
1 parent c667cc0 commit c5d5b78

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

Diff for: app_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,16 @@ func TestFlagAction(t *testing.T) {
28882888
return err
28892889
},
28902890
},
2891+
&UintSliceFlag{
2892+
Name: "f_uint_slice",
2893+
Action: func(c *Context, v []uint) error {
2894+
if len(v) > 0 && v[0] == 0 {
2895+
return fmt.Errorf("invalid uint slice")
2896+
}
2897+
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
2898+
return err
2899+
},
2900+
},
28912901
&Uint64Flag{
28922902
Name: "f_uint64",
28932903
Action: func(c *Context, v uint64) error {
@@ -2898,6 +2908,16 @@ func TestFlagAction(t *testing.T) {
28982908
return err
28992909
},
29002910
},
2911+
&Uint64SliceFlag{
2912+
Name: "f_uint64_slice",
2913+
Action: func(c *Context, v []uint64) error {
2914+
if len(v) > 0 && v[0] == 0 {
2915+
return fmt.Errorf("invalid uint64 slice")
2916+
}
2917+
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
2918+
return err
2919+
},
2920+
},
29012921
},
29022922
Action: func(ctx *Context) error { return nil },
29032923
}
@@ -3048,11 +3068,31 @@ func TestFlagAction(t *testing.T) {
30483068
args: []string{"app", "--f_uint=0"},
30493069
err: fmt.Errorf("zero uint"),
30503070
},
3071+
{
3072+
name: "flag_uint_slice",
3073+
args: []string{"app", "--f_uint_slice=1,2,3"},
3074+
exp: "[1 2 3] ",
3075+
},
3076+
{
3077+
name: "flag_uint_slice",
3078+
args: []string{"app", "--f_uint_slice=0"},
3079+
err: fmt.Errorf("invalid uint slice"),
3080+
},
30513081
{
30523082
name: "flag_uint64",
30533083
args: []string{"app", "--f_uint64=1"},
30543084
exp: "1 ",
30553085
},
3086+
{
3087+
name: "flag_uint64_slice",
3088+
args: []string{"app", "--f_uint64_slice=1,2,3"},
3089+
exp: "[1 2 3] ",
3090+
},
3091+
{
3092+
name: "flag_uint64_slice",
3093+
args: []string{"app", "--f_uint64_slice=0"},
3094+
err: fmt.Errorf("invalid uint64 slice"),
3095+
},
30563096
{
30573097
name: "flag_uint64_error",
30583098
args: []string{"app", "--f_uint64=0"},

Diff for: flag_uint64_slice.go

+9
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ func (f *Uint64SliceFlag) Get(ctx *Context) []uint64 {
190190
return ctx.Uint64Slice(f.Name)
191191
}
192192

193+
// RunAction executes flag action if set
194+
func (f *Uint64SliceFlag) RunAction(c *Context) error {
195+
if f.Action != nil {
196+
return f.Action(c, c.Uint64Slice(f.Name))
197+
}
198+
199+
return nil
200+
}
201+
193202
// Uint64Slice looks up the value of a local Uint64SliceFlag, returns
194203
// nil if not found
195204
func (cCtx *Context) Uint64Slice(name string) []uint64 {

Diff for: flag_uint_slice.go

+9
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ func (f *UintSliceFlag) Get(ctx *Context) []uint {
201201
return ctx.UintSlice(f.Name)
202202
}
203203

204+
// RunAction executes flag action if set
205+
func (f *UintSliceFlag) RunAction(c *Context) error {
206+
if f.Action != nil {
207+
return f.Action(c, c.UintSlice(f.Name))
208+
}
209+
210+
return nil
211+
}
212+
204213
// UintSlice looks up the value of a local UintSliceFlag, returns
205214
// nil if not found
206215
func (cCtx *Context) UintSlice(name string) []uint {

Diff for: godoc-current.txt

+6
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,9 @@ func (f *Uint64SliceFlag) IsVisible() bool
21462146
func (f *Uint64SliceFlag) Names() []string
21472147
Names returns the names of the flag
21482148

2149+
func (f *Uint64SliceFlag) RunAction(c *Context) error
2150+
RunAction executes flag action if set
2151+
21492152
func (f *Uint64SliceFlag) String() string
21502153
String returns a readable representation of this value (for usage defaults)
21512154

@@ -2311,6 +2314,9 @@ func (f *UintSliceFlag) IsVisible() bool
23112314
func (f *UintSliceFlag) Names() []string
23122315
Names returns the names of the flag
23132316

2317+
func (f *UintSliceFlag) RunAction(c *Context) error
2318+
RunAction executes flag action if set
2319+
23142320
func (f *UintSliceFlag) String() string
23152321
String returns a readable representation of this value (for usage defaults)
23162322

Diff for: testdata/godoc-v2.x.txt

+6
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,9 @@ func (f *Uint64SliceFlag) IsVisible() bool
21462146
func (f *Uint64SliceFlag) Names() []string
21472147
Names returns the names of the flag
21482148

2149+
func (f *Uint64SliceFlag) RunAction(c *Context) error
2150+
RunAction executes flag action if set
2151+
21492152
func (f *Uint64SliceFlag) String() string
21502153
String returns a readable representation of this value (for usage defaults)
21512154

@@ -2311,6 +2314,9 @@ func (f *UintSliceFlag) IsVisible() bool
23112314
func (f *UintSliceFlag) Names() []string
23122315
Names returns the names of the flag
23132316

2317+
func (f *UintSliceFlag) RunAction(c *Context) error
2318+
RunAction executes flag action if set
2319+
23142320
func (f *UintSliceFlag) String() string
23152321
String returns a readable representation of this value (for usage defaults)
23162322

0 commit comments

Comments
 (0)