Merge pull request #40 from codegangsta/JMS-39

App.Run() now returns an error if there was one. No more calls to os.Exit()
main
Jeremy Saenz 11 years ago
commit 4f38b0cbbf

@ -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,24 +54,29 @@ 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) if checkHelp(context) {
checkVersion(context) return nil
}
if checkVersion(context) {
return nil
}
args := context.Args() args := context.Args()
if len(args) > 0 { if len(args) > 0 {
name := args[0] name := args[0]
c := a.Command(name) c := a.Command(name)
if c != nil { if c != nil {
c.Run(context) return c.Run(context)
return
} }
} }
// 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

@ -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")
} }

@ -3,7 +3,6 @@ package cli
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"strings" "strings"
) )
@ -24,7 +23,7 @@ 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) { func (c Command) Run(ctx *Context) error {
// append help to flags // append help to flags
c.Flags = append( c.Flags = append(
c.Flags, c.Flags,
@ -55,12 +54,15 @@ func (c Command) Run(ctx *Context) {
fmt.Println("Incorrect Usage.\n") fmt.Println("Incorrect Usage.\n")
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
fmt.Println("") fmt.Println("")
os.Exit(1) return err
} }
context := NewContext(ctx.App, set, ctx.globalSet) context := NewContext(ctx.App, set, ctx.globalSet)
checkCommandHelp(context, c.Name) if checkCommandHelp(context, c.Name) {
return nil
}
c.Action(context) c.Action(context)
return nil
} }
// Returns true if Command.Name or Command.ShortName matches given name // Returns true if Command.Name or Command.ShortName matches given name

@ -73,7 +73,6 @@ func ShowCommandHelp(c *Context, command string) {
} }
fmt.Printf("No help topic for '%v'\n", command) fmt.Printf("No help topic for '%v'\n", command)
os.Exit(1)
} }
// Prints the version number of the App // Prints the version number of the App
@ -88,23 +87,29 @@ func printHelp(templ string, data interface{}) {
w.Flush() w.Flush()
} }
func checkVersion(c *Context) { func checkVersion(c *Context) bool {
if c.GlobalBool("version") { if c.GlobalBool("version") {
ShowVersion(c) ShowVersion(c)
os.Exit(0) return true
} }
return false
} }
func checkHelp(c *Context) { func checkHelp(c *Context) bool {
if c.GlobalBool("h") || c.GlobalBool("help") { if c.GlobalBool("h") || c.GlobalBool("help") {
ShowAppHelp(c) ShowAppHelp(c)
os.Exit(0) return true
} }
return false
} }
func checkCommandHelp(c *Context, name string) { func checkCommandHelp(c *Context, name string) bool {
if c.Bool("h") || c.Bool("help") { if c.Bool("h") || c.Bool("help") {
ShowCommandHelp(c, name) ShowCommandHelp(c, name)
os.Exit(0) return true
} }
return false
} }

Loading…
Cancel
Save