diff --git a/cobra.go b/cobra.go index cb6501d3..039c1853 100644 --- a/cobra.go +++ b/cobra.go @@ -25,11 +25,12 @@ import ( "text/template" ) -// Called after flags are parsed immediately before executing any Command -var InitializeConfig func() +var initializers []func() -func init() { - InitializeConfig = func() {} +func OnInitialize(y ...func()) { + for _, x := range y { + initializers = append(initializers, x) + } } func Gt(a interface{}, b interface{}) bool { diff --git a/command.go b/command.go index 4a03a031..dece3258 100644 --- a/command.go +++ b/command.go @@ -358,7 +358,9 @@ func (c *Command) execute(a []string) (err error) { } func (c *Command) preRun() { - InitializeConfig() + for _, x := range initializers { + x() + } } func (c *Command) errorMsgFromParse() string {