From 0cdd1b3479664c366657d546c54013da6ad18799 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 9 Aug 2021 17:13:28 -0400 Subject: [PATCH] Add EnableAlwaysReplaceContexts --- cobra.go | 5 +++++ command.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cobra.go b/cobra.go index d6cbfd71..ec4c5757 100644 --- a/cobra.go +++ b/cobra.go @@ -49,6 +49,11 @@ var EnablePrefixMatching = false // To disable sorting, set it to false. var EnableCommandSorting = true +// EnableAlwaysReplaceContexts changes the behaviour of contexts being passed to child commands. By default a child +// command's context will be set to its parents context on the first run, but not subsequent runs. +// To pass contexts to children always, set this to true. +var EnableAlwaysReplaceContexts = false + // MousetrapHelpText enables an information splash screen on Windows // if the CLI is started from explorer.exe. // To disable the mousetrap, just set this variable to blank string (""). diff --git a/command.go b/command.go index 2cc18891..b71b9f9d 100644 --- a/command.go +++ b/command.go @@ -967,7 +967,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { // We have to pass global context to children command // if context is present on the parent command. - if cmd.ctx == nil { + if cmd.ctx == nil || EnableAlwaysReplaceContexts { cmd.ctx = c.ctx }