JMS #39: App.Run returns an error

This commit is contained in:
Jeremy Saenz 2013-11-01 06:23:19 -07:00
parent 0d3c3f4497
commit e6e641143c
2 changed files with 8 additions and 5 deletions

7
app.go
View File

@ -34,7 +34,7 @@ func NewApp() *App {
} }
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination // Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) { func (a *App) Run(arguments []string) error {
// append help to commands // append help to commands
if a.Command(helpCommand.Name) == nil { if a.Command(helpCommand.Name) == nil {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
@ -54,7 +54,7 @@ func (a *App) Run(arguments []string) {
fmt.Println("Incorrect Usage.\n") fmt.Println("Incorrect Usage.\n")
ShowAppHelp(context) ShowAppHelp(context)
fmt.Println("") fmt.Println("")
os.Exit(1) return err
} }
checkHelp(context) checkHelp(context)
@ -66,12 +66,13 @@ func (a *App) Run(arguments []string) {
c := a.Command(name) c := a.Command(name)
if c != nil { if c != nil {
c.Run(context) c.Run(context)
return return nil
} }
} }
// Run default Action // Run default Action
a.Action(context) a.Action(context)
return nil
} }
// Returns the named command on App. Returns nil if the command does not exist // Returns the named command on App. Returns nil if the command does not exist

View File

@ -32,8 +32,10 @@ func TestApp_Run(t *testing.T) {
s = s + c.Args()[0] s = s + c.Args()[0]
} }
app.Run([]string{"command", "foo"}) err := app.Run([]string{"command", "foo"})
app.Run([]string{"command", "bar"}) expect(t, err, nil)
err = app.Run([]string{"command", "bar"})
expect(t, err, nil)
expect(t, s, "foobar") expect(t, s, "foobar")
} }