StringSliceFlag.ValueFromContext() as convenient accessor
This commit is contained in:
parent
ce4d9279c4
commit
6b336c478f
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user