Repeat context statement because of a is a pointer

performance  optimization
gofmt  code
This commit is contained in:
leonardyp 2016-02-04 15:25:41 +08:00
parent cf1f63a727
commit 6f6e8caf6c
3 changed files with 11 additions and 19 deletions

17
app.go
View File

@ -119,21 +119,19 @@ func (a *App) Run(arguments []string) (err error) {
set.SetOutput(ioutil.Discard) set.SetOutput(ioutil.Discard)
err = set.Parse(arguments[1:]) err = set.Parse(arguments[1:])
nerr := normalizeFlags(a.Flags, set) nerr := normalizeFlags(a.Flags, set)
context := NewContext(a, set, nil)
if nerr != nil { if nerr != nil {
fmt.Fprintln(a.Writer, nerr) fmt.Fprintln(a.Writer, nerr)
context := NewContext(a, set, nil)
ShowAppHelp(context) ShowAppHelp(context)
return nerr return nerr
} }
context := NewContext(a, set, nil)
if checkCompletions(context) { if checkCompletions(context) {
return nil return nil
} }
if err != nil { if err != nil {
fmt.Fprintln(a.Writer, "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
fmt.Fprintln(a.Writer)
ShowAppHelp(context) ShowAppHelp(context)
return err return err
} }
@ -150,8 +148,7 @@ func (a *App) Run(arguments []string) (err error) {
if a.After != nil { if a.After != nil {
defer func() { defer func() {
afterErr := a.After(context) if afterErr := a.After(context); afterErr != nil {
if afterErr != nil {
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
@ -162,10 +159,9 @@ func (a *App) Run(arguments []string) (err error) {
} }
if a.Before != nil { if a.Before != nil {
err := a.Before(context) err = a.Before(context)
if err != nil { if err != nil {
fmt.Fprintln(a.Writer, err) fmt.Fprintf(a.Writer, "%v\n\n", err)
fmt.Fprintln(a.Writer)
ShowAppHelp(context) ShowAppHelp(context)
return err return err
} }
@ -242,8 +238,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
} }
if err != nil { if err != nil {
fmt.Fprintln(a.Writer, "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
fmt.Fprintln(a.Writer)
ShowSubcommandHelp(context) ShowSubcommandHelp(context)
return err return err
} }

View File

@ -192,7 +192,7 @@ func (c Command) startApp(ctx *Context) error {
if c.HelpName == "" { if c.HelpName == "" {
app.HelpName = c.HelpName app.HelpName = c.HelpName
} else { } else {
app.HelpName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) app.HelpName = app.Name
} }
if c.Description != "" { if c.Description != "" {
@ -231,12 +231,9 @@ func (c Command) startApp(ctx *Context) error {
app.Action = helpSubcommand.Action app.Action = helpSubcommand.Action
} }
var newCmds []Command for index, cc := range app.Commands {
for _, cc := range app.Commands { app.Commands[index].commandNamePath = []string{c.Name, cc.Name}
cc.commandNamePath = []string{c.Name, cc.Name}
newCmds = append(newCmds, cc)
} }
app.Commands = newCmds
return app.RunAsSubcommand(ctx) return app.RunAsSubcommand(ctx)
} }

View File

@ -3,10 +3,10 @@ package cli
import ( import (
"errors" "errors"
"flag" "flag"
"io/ioutil"
"testing"
"fmt" "fmt"
"io/ioutil"
"strings" "strings"
"testing"
) )
func TestCommandFlagParsing(t *testing.T) { func TestCommandFlagParsing(t *testing.T) {