Skip to content

Commit e855c4c

Browse files
fix: Nil pointer when getting context's flagSet(#1325) (#1327)
* fix: Nil pointer when getting context's flagSet in UsageError function(#1325) * test: add unit test for nil flagset in Context
1 parent c2cf7ed commit e855c4c

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ func (ctx *Context) lookupFlag(name string) Flag {
151151

152152
func (ctx *Context) lookupFlagSet(name string) *flag.FlagSet {
153153
for _, c := range ctx.Lineage() {
154+
if c.flagSet == nil {
155+
continue
156+
}
154157
if f := c.flagSet.Lookup(name); f != nil {
155158
return c.flagSet
156159
}

context_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func TestContext_String(t *testing.T) {
112112
c := NewContext(nil, set, parentCtx)
113113
expect(t, c.String("myflag"), "hello world")
114114
expect(t, c.String("top-flag"), "hai veld")
115+
c = NewContext(nil, nil, parentCtx)
116+
expect(t, c.String("top-flag"), "hai veld")
115117
}
116118

117119
func TestContext_Path(t *testing.T) {

0 commit comments

Comments
 (0)