diff --git a/command.go b/command.go index 8b2a691..d262c39 100644 --- a/command.go +++ b/command.go @@ -227,6 +227,9 @@ type Command struct { // Context returns underlying command context. If command wasn't // executed with ExecuteContext Context returns Background context. func (c *Command) Context() context.Context { + if c.ctx == nil { + c.ctx = context.Background() + } return c.ctx } diff --git a/command_test.go b/command_test.go index 32e15c2..3e16cad 100644 --- a/command_test.go +++ b/command_test.go @@ -2080,3 +2080,10 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) { } checkStringContains(t, output, "unknown flag: --unknown") } + +func TestContext(t *testing.T){ + root := &Command{} + if root.Context() == nil { + t.Error("expected root.Context() != nil") + } +}