diff --git a/app.go b/app.go index 6884920..d3db6ab 100644 --- a/app.go +++ b/app.go @@ -119,21 +119,19 @@ func (a *App) Run(arguments []string) (err error) { set.SetOutput(ioutil.Discard) err = set.Parse(arguments[1:]) nerr := normalizeFlags(a.Flags, set) + context := NewContext(a, set, nil) if nerr != nil { fmt.Fprintln(a.Writer, nerr) - context := NewContext(a, set, nil) ShowAppHelp(context) return nerr } - context := NewContext(a, set, nil) if checkCompletions(context) { return nil } if err != nil { - fmt.Fprintln(a.Writer, "Incorrect Usage.") - fmt.Fprintln(a.Writer) + fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") ShowAppHelp(context) return err } @@ -150,8 +148,7 @@ func (a *App) Run(arguments []string) (err error) { if a.After != nil { defer func() { - afterErr := a.After(context) - if afterErr != nil { + if afterErr := a.After(context); afterErr != nil { if err != nil { err = NewMultiError(err, afterErr) } else { @@ -162,10 +159,9 @@ func (a *App) Run(arguments []string) (err error) { } if a.Before != nil { - err := a.Before(context) + err = a.Before(context) if err != nil { - fmt.Fprintln(a.Writer, err) - fmt.Fprintln(a.Writer) + fmt.Fprintf(a.Writer, "%v\n\n", err) ShowAppHelp(context) return err } @@ -242,8 +238,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { } if err != nil { - fmt.Fprintln(a.Writer, "Incorrect Usage.") - fmt.Fprintln(a.Writer) + fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") ShowSubcommandHelp(context) return err } diff --git a/command.go b/command.go index aebb4bb..077d8e2 100644 --- a/command.go +++ b/command.go @@ -192,7 +192,7 @@ func (c Command) startApp(ctx *Context) error { if c.HelpName == "" { app.HelpName = c.HelpName } else { - app.HelpName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) + app.HelpName = app.Name } if c.Description != "" { @@ -231,12 +231,9 @@ func (c Command) startApp(ctx *Context) error { app.Action = helpSubcommand.Action } - var newCmds []Command - for _, cc := range app.Commands { - cc.commandNamePath = []string{c.Name, cc.Name} - newCmds = append(newCmds, cc) + for index, cc := range app.Commands { + app.Commands[index].commandNamePath = []string{c.Name, cc.Name} } - app.Commands = newCmds return app.RunAsSubcommand(ctx) } diff --git a/command_test.go b/command_test.go index 50bd875..536392f 100644 --- a/command_test.go +++ b/command_test.go @@ -3,10 +3,10 @@ package cli import ( "errors" "flag" - "io/ioutil" - "testing" "fmt" + "io/ioutil" "strings" + "testing" ) func TestCommandFlagParsing(t *testing.T) {