IntFlag.ValueFromContext() as convenient accessor
This commit is contained in:
parent
8bd5fb2390
commit
bf18c00347
@ -102,6 +102,11 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValueFromContext returns the flag’s value in the given Context.
|
||||
func (f *IntFlag) ValueFromContext(ctx *Context) int {
|
||||
return ctx.Int(f.Name)
|
||||
}
|
||||
|
||||
// Int looks up the value of a local IntFlag, returns
|
||||
// 0 if not found
|
||||
func (c *Context) Int(name string) int {
|
||||
|
@ -663,6 +663,14 @@ func TestIntFlagApply_SetsAllNames(t *testing.T) {
|
||||
expect(t, v, 5)
|
||||
}
|
||||
|
||||
func TestIntFlagValueFromContext(t *testing.T) {
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.Int("myflag", 42, "doc")
|
||||
ctx := NewContext(nil, set, nil)
|
||||
f := &IntFlag{Name: "myflag"}
|
||||
expect(t, f.ValueFromContext(ctx), 42)
|
||||
}
|
||||
|
||||
var int64FlagTests = []struct {
|
||||
name string
|
||||
expected string
|
||||
|
Loading…
Reference in New Issue
Block a user