Skip to content

Commit

Permalink
fix: Nil pointer when getting context's flagSet(#1325) (#1327)
Browse files Browse the repository at this point in the history
* fix: Nil pointer when getting context's flagSet in UsageError function(#1325)

* test: add unit test for nil flagset in Context
  • Loading branch information
WUST-mengqinyu committed Jan 17, 2022
1 parent c2cf7ed commit e855c4c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions context.go
Expand Up @@ -151,6 +151,9 @@ func (ctx *Context) lookupFlag(name string) Flag {

func (ctx *Context) lookupFlagSet(name string) *flag.FlagSet {
for _, c := range ctx.Lineage() {
if c.flagSet == nil {
continue
}
if f := c.flagSet.Lookup(name); f != nil {
return c.flagSet
}
Expand Down
2 changes: 2 additions & 0 deletions context_test.go
Expand Up @@ -112,6 +112,8 @@ func TestContext_String(t *testing.T) {
c := NewContext(nil, set, parentCtx)
expect(t, c.String("myflag"), "hello world")
expect(t, c.String("top-flag"), "hai veld")
c = NewContext(nil, nil, parentCtx)
expect(t, c.String("top-flag"), "hai veld")
}

func TestContext_Path(t *testing.T) {
Expand Down

0 comments on commit e855c4c

Please sign in to comment.