Added Before method to command. If set, or if command.Subcommands is set, then the command is treated as a recursive subcommand

This commit is contained in:
Summer Mousa 2014-04-16 15:26:28 -05:00
parent 1a63283d44
commit faf2a3d4a3
2 changed files with 15 additions and 4 deletions

7
app.go
View File

@ -171,6 +171,13 @@ func (a *App) RunAsSubcommand(c *Context) error {
return nil
}
if a.Before != nil {
err := a.Before(context)
if err != nil {
return err
}
}
args := context.Args()
if args.Present() {
name := args.First()

View File

@ -18,17 +18,20 @@ type Command struct {
Description string
// The function to call when checking for bash command completions
BashComplete func(context *Context)
// An action to execute before any sub-subcommands are run, but after the context is ready
// If a non-nil error is returned, no sub-subcommands are run
Before func(context *Context) error
// The function to call when this command is invoked
Action func(context *Context)
// List of flags to parse
Flags []Flag
// List of child commands
Subcommands []Command
// List of flags to parse
Flags []Flag
}
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) error {
if len(c.Subcommands) > 0 {
if (c.Subcommands != nil && len(c.Subcommands) > 0) || c.Before != nil {
return c.startApp(ctx)
}
@ -117,7 +120,8 @@ func (c Command) startApp(ctx *Context) error {
app.BashComplete = c.BashComplete
}
// set the action
// set the actions
app.Before = c.Before
if c.Action != nil {
app.Action = c.Action
} else {