Float64SliceFlag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 16:04:42 +02:00
parent 5047beb001
commit 2f92fc644c
2 changed files with 13 additions and 0 deletions

View File

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

View File

@ -1117,6 +1117,14 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestFloat64SliceFlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Var(NewFloat64Slice(1.23, 4.56), "myflag", "doc")
ctx := NewContext(nil, set, nil)
f := &Float64SliceFlag{Name: "myflag"}
expect(t, f.ValueFromContext(ctx), []float64{1.23, 4.56})
}
var genericFlagTests = []struct { var genericFlagTests = []struct {
name string name string
value Generic value Generic