IntSliceFlag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 16:49:38 +02:00
parent dcc47855b8
commit 6d7f859008
2 changed files with 13 additions and 0 deletions

View File

@ -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 {

View File

@ -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