Skip to content

Commit 2f92fc6

Browse files
committed
Float64SliceFlag.ValueFromContext() as convenient accessor
1 parent 5047beb commit 2f92fc6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

flag_float64_slice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
175175
return nil
176176
}
177177

178+
// ValueFromContext returns the flag’s value in the given Context.
179+
func (f *Float64SliceFlag) ValueFromContext(ctx *Context) []float64 {
180+
return ctx.Float64Slice(f.Name)
181+
}
182+
178183
// Float64Slice looks up the value of a local Float64SliceFlag, returns
179184
// nil if not found
180185
func (c *Context) Float64Slice(name string) []float64 {

flag_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,14 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
11171117
}
11181118
}
11191119

1120+
func TestFloat64SliceFlagValueFromContext(t *testing.T) {
1121+
set := flag.NewFlagSet("test", 0)
1122+
set.Var(NewFloat64Slice(1.23, 4.56), "myflag", "doc")
1123+
ctx := NewContext(nil, set, nil)
1124+
f := &Float64SliceFlag{Name: "myflag"}
1125+
expect(t, f.ValueFromContext(ctx), []float64{1.23, 4.56})
1126+
}
1127+
11201128
var genericFlagTests = []struct {
11211129
name string
11221130
value Generic

0 commit comments

Comments
 (0)