From 864687ae689edc28688c67edef47e3d2ad651a1b Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 11 Jul 2014 10:57:53 -0400 Subject: [PATCH] Adding "OnInitialize()" method instead of directly setting a property. Now can have multiple initializers. --- cobra.go | 9 +++++---- command.go | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cobra.go b/cobra.go index cb6501d..039c185 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 4a03a03..dece325 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 {