From e855c4cd1d5f87ae2a12c0e2620c0b747101c2bc Mon Sep 17 00:00:00 2001 From: badcw Date: Mon, 17 Jan 2022 08:22:57 +0800 Subject: [PATCH] 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 --- context.go | 3 +++ context_test.go | 2 ++ 2 files changed, 5 insertions(+) diff --git a/context.go b/context.go index 94cbb65909..da090e8256 100644 --- a/context.go +++ b/context.go @@ -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 } diff --git a/context_test.go b/context_test.go index b37876c033..84757063eb 100644 --- a/context_test.go +++ b/context_test.go @@ -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) {