From 2535376782cb8bf244a767afd45250ce7fc8b326 Mon Sep 17 00:00:00 2001 From: Summer Mousa Date: Thu, 17 Apr 2014 11:48:00 -0500 Subject: [PATCH] If the Subcommand is instantiated, via the Before method and has no subcommands, display the CommandHelp instead of the SubcommandHelp --- app.go | 36 ++++++++++++++++++++++++++---------- command.go | 3 ++- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index a4a76ef..a792705 100644 --- a/app.go +++ b/app.go @@ -130,13 +130,15 @@ func (a *App) Run(arguments []string) error { } // Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags -func (a *App) RunAsSubcommand(c *Context) error { +func (a *App) RunAsSubcommand(ctx *Context) error { // append help to commands - if a.Command(helpCommand.Name) == nil { - a.Commands = append(a.Commands, helpCommand) + if len(a.Commands) > 0 { + if a.Command(helpCommand.Name) == nil { + a.Commands = append(a.Commands, helpCommand) + } } - // append help flags + // append flags if a.EnableBashCompletion { a.appendFlag(BashCompletionFlag) } @@ -145,13 +147,17 @@ func (a *App) RunAsSubcommand(c *Context) error { // parse flags set := flagSet(a.Name, a.Flags) set.SetOutput(ioutil.Discard) - err := set.Parse(c.Args().Tail()) + err := set.Parse(ctx.Args().Tail()) nerr := normalizeFlags(a.Flags, set) context := NewContext(a, set, set) if nerr != nil { fmt.Println(nerr) - ShowSubcommandHelp(context) + if len(a.Commands) > 0 { + ShowSubcommandHelp(context) + } else { + ShowCommandHelp(ctx, context.Args().First()) + } fmt.Println("") return nerr } @@ -159,7 +165,6 @@ func (a *App) RunAsSubcommand(c *Context) error { if err != nil { fmt.Printf("Incorrect Usage.\n\n") ShowSubcommandHelp(context) - fmt.Println("") return err } @@ -167,8 +172,14 @@ func (a *App) RunAsSubcommand(c *Context) error { return nil } - if checkSubcommandHelp(context) { - return nil + if len(a.Commands) > 0 { + if checkSubcommandHelp(context) { + return nil + } + } else { + if checkCommandHelp(ctx, context.Args().First()) { + return nil + } } if a.Before != nil { @@ -188,7 +199,12 @@ func (a *App) RunAsSubcommand(c *Context) error { } // Run default Action - a.Action(context) + if len(a.Commands) > 0 { + a.Action(context) + } else { + a.Action(ctx) + } + return nil } diff --git a/command.go b/command.go index 735505b..cbf6e24 100644 --- a/command.go +++ b/command.go @@ -31,7 +31,8 @@ type Command struct { // Invokes the command given the context, parses ctx.Args() to generate command-specific flags func (c Command) Run(ctx *Context) error { - if (c.Subcommands != nil && len(c.Subcommands) > 0) || c.Before != nil { + + if len(c.Subcommands) > 0 || c.Before != nil { return c.startApp(ctx) }