Merge pull request #1210 from jcgregorio/fix-context-value
Fix Context.Value.
This commit is contained in:
commit
5378891c91
@ -108,7 +108,10 @@ func (c *Context) Lineage() []*Context {
|
|||||||
|
|
||||||
// Value returns the value of the flag corresponding to `name`
|
// Value returns the value of the flag corresponding to `name`
|
||||||
func (c *Context) Value(name string) interface{} {
|
func (c *Context) Value(name string) interface{} {
|
||||||
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
|
return fs.Lookup(name).Value.(flag.Getter).Get()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Args returns the command line arguments associated with the context.
|
// Args returns the command line arguments associated with the context.
|
||||||
|
@ -136,6 +136,18 @@ func TestContext_Bool(t *testing.T) {
|
|||||||
expect(t, c.Bool("top-flag"), true)
|
expect(t, c.Bool("top-flag"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext_Value(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Int("myflag", 12, "doc")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Int("top-flag", 13, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
|
expect(t, c.Value("myflag"), 12)
|
||||||
|
expect(t, c.Value("top-flag"), 13)
|
||||||
|
expect(t, c.Value("unknown-flag"), nil)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext_Args(t *testing.T) {
|
func TestContext_Args(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
|
Loading…
Reference in New Issue
Block a user