diff --git a/flag_string_slice.go b/flag_string_slice.go index 1664247..90e6ebd 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -186,6 +186,11 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *StringSliceFlag) ValueFromContext(ctx *Context) []string { + return ctx.StringSlice(f.Name) +} + // StringSlice looks up the value of a local StringSliceFlag, returns // nil if not found func (c *Context) StringSlice(name string) []string { diff --git a/flag_test.go b/flag_test.go index aa9fbec..cda34be 100644 --- a/flag_test.go +++ b/flag_test.go @@ -630,6 +630,14 @@ func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) { expect(t, defValue, fl.Destination.Value()) } +func TestStringSliceFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(NewStringSlice("a", "b", "c"), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &StringSliceFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), []string{"a", "b", "c"}) +} + var intFlagTests = []struct { name string expected string