mirror of https://github.com/spf13/cobra.git
Adding "OnInitialize()" method instead of directly setting a property. Now can have multiple initializers.
This commit is contained in:
parent
7cebca3761
commit
864687ae68
9
cobra.go
9
cobra.go
|
@ -25,11 +25,12 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Called after flags are parsed immediately before executing any Command
|
var initializers []func()
|
||||||
var InitializeConfig func()
|
|
||||||
|
|
||||||
func init() {
|
func OnInitialize(y ...func()) {
|
||||||
InitializeConfig = func() {}
|
for _, x := range y {
|
||||||
|
initializers = append(initializers, x)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Gt(a interface{}, b interface{}) bool {
|
func Gt(a interface{}, b interface{}) bool {
|
||||||
|
|
|
@ -358,7 +358,9 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) preRun() {
|
func (c *Command) preRun() {
|
||||||
InitializeConfig()
|
for _, x := range initializers {
|
||||||
|
x()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) errorMsgFromParse() string {
|
func (c *Command) errorMsgFromParse() string {
|
||||||
|
|
Loading…
Reference in New Issue