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
main
badcw 2 years ago committed by GitHub
parent c2cf7ed6df
commit e855c4cd1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

Loading…
Cancel
Save