From dcc47855b8d7685e47644ee6966df6dc7de8dfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:45:10 +0200 Subject: [PATCH] Int64SliceFlag.ValueFromContext() as convenient accessor --- flag_int64_slice.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_int64_slice.go b/flag_int64_slice.go index a53b185..29e8dc4 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -174,6 +174,11 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s 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 { diff --git a/flag_test.go b/flag_test.go index 0b343e9..d510235 100644 --- a/flag_test.go +++ b/flag_test.go @@ -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