Uint64Flag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 17:11:41 +02:00
parent 1f621059d3
commit ca7f26ecb0
2 changed files with 13 additions and 0 deletions

View File

@ -825,6 +825,14 @@ func TestUint64FlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestUint64FlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Uint64("myflag", 42, "doc")
ctx := NewContext(nil, set, nil)
f := &Uint64Flag{Name: "myflag"}
expect(t, f.ValueFromContext(ctx), uint64(42))
}
var durationFlagTests = []struct { var durationFlagTests = []struct {
name string name string
expected string expected string

View File

@ -101,6 +101,11 @@ func (f *Uint64Flag) GetEnvVars() []string {
return f.EnvVars return f.EnvVars
} }
// ValueFromContext returns the flags value in the given Context.
func (f *Uint64Flag) ValueFromContext(ctx *Context) uint64 {
return ctx.Uint64(f.Name)
}
// Uint64 looks up the value of a local Uint64Flag, returns // Uint64 looks up the value of a local Uint64Flag, returns
// 0 if not found // 0 if not found
func (c *Context) Uint64(name string) uint64 { func (c *Context) Uint64(name string) uint64 {