If the Subcommand is instantiated, via the Before method and has no subcommands, display the CommandHelp instead of the SubcommandHelp
This commit is contained in:
parent
faf2a3d4a3
commit
2535376782
36
app.go
36
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
|
// 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
|
// append help to commands
|
||||||
if a.Command(helpCommand.Name) == nil {
|
if len(a.Commands) > 0 {
|
||||||
a.Commands = append(a.Commands, helpCommand)
|
if a.Command(helpCommand.Name) == nil {
|
||||||
|
a.Commands = append(a.Commands, helpCommand)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// append help flags
|
// append flags
|
||||||
if a.EnableBashCompletion {
|
if a.EnableBashCompletion {
|
||||||
a.appendFlag(BashCompletionFlag)
|
a.appendFlag(BashCompletionFlag)
|
||||||
}
|
}
|
||||||
@ -145,13 +147,17 @@ func (a *App) RunAsSubcommand(c *Context) error {
|
|||||||
// parse flags
|
// parse flags
|
||||||
set := flagSet(a.Name, a.Flags)
|
set := flagSet(a.Name, a.Flags)
|
||||||
set.SetOutput(ioutil.Discard)
|
set.SetOutput(ioutil.Discard)
|
||||||
err := set.Parse(c.Args().Tail())
|
err := set.Parse(ctx.Args().Tail())
|
||||||
nerr := normalizeFlags(a.Flags, set)
|
nerr := normalizeFlags(a.Flags, set)
|
||||||
context := NewContext(a, set, set)
|
context := NewContext(a, set, set)
|
||||||
|
|
||||||
if nerr != nil {
|
if nerr != nil {
|
||||||
fmt.Println(nerr)
|
fmt.Println(nerr)
|
||||||
ShowSubcommandHelp(context)
|
if len(a.Commands) > 0 {
|
||||||
|
ShowSubcommandHelp(context)
|
||||||
|
} else {
|
||||||
|
ShowCommandHelp(ctx, context.Args().First())
|
||||||
|
}
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
@ -159,7 +165,6 @@ func (a *App) RunAsSubcommand(c *Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Incorrect Usage.\n\n")
|
fmt.Printf("Incorrect Usage.\n\n")
|
||||||
ShowSubcommandHelp(context)
|
ShowSubcommandHelp(context)
|
||||||
fmt.Println("")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,8 +172,14 @@ func (a *App) RunAsSubcommand(c *Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkSubcommandHelp(context) {
|
if len(a.Commands) > 0 {
|
||||||
return nil
|
if checkSubcommandHelp(context) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if checkCommandHelp(ctx, context.Args().First()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.Before != nil {
|
if a.Before != nil {
|
||||||
@ -188,7 +199,12 @@ func (a *App) RunAsSubcommand(c *Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run default Action
|
// Run default Action
|
||||||
a.Action(context)
|
if len(a.Commands) > 0 {
|
||||||
|
a.Action(context)
|
||||||
|
} else {
|
||||||
|
a.Action(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,8 @@ type Command struct {
|
|||||||
|
|
||||||
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||||
func (c Command) Run(ctx *Context) error {
|
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)
|
return c.startApp(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user