Skip to content

Commit

Permalink
Fix Context.Value.
Browse files Browse the repository at this point in the history
Before this change the added test would crash on a nil
pointer dereference because the original code would
only look in the local fileSet and not across all
the fileSets.
  • Loading branch information
jcgregorio committed Dec 7, 2020
1 parent d583313 commit 0304759
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion context.go
Expand Up @@ -108,7 +108,10 @@ func (c *Context) Lineage() []*Context {

// Value returns the value of the flag corresponding to `name`
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.
Expand Down
11 changes: 11 additions & 0 deletions context_test.go
Expand Up @@ -136,6 +136,17 @@ func TestContext_Bool(t *testing.T) {
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)
}

func TestContext_Args(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
Expand Down

0 comments on commit 0304759

Please sign in to comment.