mirror of https://github.com/spf13/cobra.git
added pre and post run hooks.
This commit is contained in:
parent
9cf0f3737d
commit
2df64026ba
16
command.go
16
command.go
|
@ -59,6 +59,12 @@ type Command struct {
|
||||||
// Run runs the command.
|
// Run runs the command.
|
||||||
// The args are the arguments after the command name.
|
// The args are the arguments after the command name.
|
||||||
Run func(cmd *Command, args []string)
|
Run func(cmd *Command, args []string)
|
||||||
|
// PreRun runs the command after the flags are parsed and before run.
|
||||||
|
// The args are the arguments after the command name.
|
||||||
|
PreRun func(cmd *Command, args []string)
|
||||||
|
// PostRun runs the command after run.
|
||||||
|
// The args are the arguments after the command name.
|
||||||
|
PostRun func(cmd *Command, args []string)
|
||||||
// Commands is the list of commands supported by this program.
|
// Commands is the list of commands supported by this program.
|
||||||
commands []*Command
|
commands []*Command
|
||||||
// Parent Command for this command
|
// Parent Command for this command
|
||||||
|
@ -448,7 +454,17 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
argWoFlags := c.Flags().Args()
|
argWoFlags := c.Flags().Args()
|
||||||
|
|
||||||
|
if c.PreRun != nil {
|
||||||
|
c.PreRun(c, argWoFlags)
|
||||||
|
}
|
||||||
|
|
||||||
c.Run(c, argWoFlags)
|
c.Run(c, argWoFlags)
|
||||||
|
|
||||||
|
if c.PostRun != nil {
|
||||||
|
c.PostRun(c, argWoFlags)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue