IntSliceFlag.ValueFromContext() as convenient accessor

main
Tilo Prütz 2 years ago
parent dcc47855b8
commit 6d7f859008

@ -185,6 +185,11 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
return nil
}
// ValueFromContext returns the flags value in the given Context.
func (f *IntSliceFlag) ValueFromContext(ctx *Context) []int {
return ctx.IntSlice(f.Name)
}
// IntSlice looks up the value of a local IntSliceFlag, returns
// nil if not found
func (c *Context) IntSlice(name string) []int {

@ -939,6 +939,14 @@ func TestIntSliceFlag_SetFromParentContext(t *testing.T) {
}
}
func TestIntSliceFlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Var(NewIntSlice(1, 2, 3), "myflag", "doc")
ctx := NewContext(nil, set, nil)
f := &IntSliceFlag{Name: "myflag"}
expect(t, f.ValueFromContext(ctx), []int{1, 2, 3})
}
var int64SliceFlagTests = []struct {
name string
aliases []string

Loading…
Cancel
Save