Skip to content

Commit 6d7f859

Browse files
committed
IntSliceFlag.ValueFromContext() as convenient accessor
1 parent dcc4785 commit 6d7f859

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

flag_int_slice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
185185
return nil
186186
}
187187

188+
// ValueFromContext returns the flag’s value in the given Context.
189+
func (f *IntSliceFlag) ValueFromContext(ctx *Context) []int {
190+
return ctx.IntSlice(f.Name)
191+
}
192+
188193
// IntSlice looks up the value of a local IntSliceFlag, returns
189194
// nil if not found
190195
func (c *Context) IntSlice(name string) []int {

flag_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,14 @@ func TestIntSliceFlag_SetFromParentContext(t *testing.T) {
939939
}
940940
}
941941

942+
func TestIntSliceFlagValueFromContext(t *testing.T) {
943+
set := flag.NewFlagSet("test", 0)
944+
set.Var(NewIntSlice(1, 2, 3), "myflag", "doc")
945+
ctx := NewContext(nil, set, nil)
946+
f := &IntSliceFlag{Name: "myflag"}
947+
expect(t, f.ValueFromContext(ctx), []int{1, 2, 3})
948+
}
949+
942950
var int64SliceFlagTests = []struct {
943951
name string
944952
aliases []string

0 commit comments

Comments
 (0)