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
This commit is contained in:
badcw 2022-01-17 08:22:57 +08:00 committed by GitHub
parent c2cf7ed6df
commit e855c4cd1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {