Float64Flag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 15:55:37 +02:00
parent 9eae255aac
commit 5047beb001
2 changed files with 13 additions and 0 deletions

View File

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

View File

@ -1068,6 +1068,14 @@ func TestFloat64FlagApply_SetsAllNames(t *testing.T) {
expect(t, v, float64(43.33333)) expect(t, v, float64(43.33333))
} }
func TestFloat64FlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Float64("myflag", 1.23, "doc")
ctx := NewContext(nil, set, nil)
f := &Float64Flag{Name: "myflag"}
expect(t, f.ValueFromContext(ctx), 1.23)
}
var float64SliceFlagTests = []struct { var float64SliceFlagTests = []struct {
name string name string
aliases []string aliases []string