diff --git a/command.go b/command.go index 2cc18891..ee34f392 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 ca90e67c..7878c297 100644 --- a/command_test.go +++ b/command_test.go @@ -2058,3 +2058,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") + } +}