Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Uint64Flag.ValueFromContext() as convenient accessor
  • Loading branch information
toaster committed Apr 22, 2022
commit ca7f26ecb04f8092897b05cdc2ccb03827a15da9
8 changes: 8 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
name string
expected string
Expand Down
5 changes: 5 additions & 0 deletions flag_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (f *Uint64Flag) GetEnvVars() []string {
return f.EnvVars
}

// ValueFromContext returns the flag’s 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
// 0 if not found
func (c *Context) Uint64(name string) uint64 {
Expand Down