StringSliceFlag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 17:04:13 +02:00
parent ce4d9279c4
commit 6b336c478f
2 changed files with 13 additions and 0 deletions

View File

@ -186,6 +186,11 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
return nil
}
// ValueFromContext returns the flags 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 {

View File

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