Int64SliceFlag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 16:45:10 +02:00
parent 18b44dfb29
commit dcc47855b8
2 changed files with 13 additions and 0 deletions

View File

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

View File

@ -1035,6 +1035,14 @@ func TestInt64SliceFlag_ReturnNil(t *testing.T) {
}
}
func TestInt64SliceFlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Var(NewInt64Slice(1, 2, 3), "myflag", "doc")
ctx := NewContext(nil, set, nil)
f := &Int64SliceFlag{Name: "myflag"}
expect(t, f.ValueFromContext(ctx), []int64{1, 2, 3})
}
var float64FlagTests = []struct {
name string
expected string