From e6e641143cf1020d57f73164eed32f38a81a534b Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Fri, 1 Nov 2013 06:23:19 -0700 Subject: [PATCH 1/2] JMS #39: App.Run returns an error --- app.go | 7 ++++--- app_test.go | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index 683f453..14730b3 100644 --- a/app.go +++ b/app.go @@ -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 -func (a *App) Run(arguments []string) { +func (a *App) Run(arguments []string) error { // append help to commands if a.Command(helpCommand.Name) == nil { a.Commands = append(a.Commands, helpCommand) @@ -54,7 +54,7 @@ func (a *App) Run(arguments []string) { fmt.Println("Incorrect Usage.\n") ShowAppHelp(context) fmt.Println("") - os.Exit(1) + return err } checkHelp(context) @@ -66,12 +66,13 @@ func (a *App) Run(arguments []string) { c := a.Command(name) if c != nil { c.Run(context) - return + return nil } } // Run default Action a.Action(context) + return nil } // Returns the named command on App. Returns nil if the command does not exist diff --git a/app_test.go b/app_test.go index 20a568a..6cc488b 100644 --- a/app_test.go +++ b/app_test.go @@ -32,8 +32,10 @@ func TestApp_Run(t *testing.T) { s = s + c.Args()[0] } - app.Run([]string{"command", "foo"}) - app.Run([]string{"command", "bar"}) + err := app.Run([]string{"command", "foo"}) + expect(t, err, nil) + err = app.Run([]string{"command", "bar"}) + expect(t, err, nil) expect(t, s, "foobar") } From b25b7a883c89175a9f374409a2b973643b316f22 Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Fri, 1 Nov 2013 06:45:19 -0700 Subject: [PATCH 2/2] JMS #39: Removed all calls to os.Exit(). --- app.go | 12 ++++++++---- command.go | 10 ++++++---- help.go | 19 ++++++++++++------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app.go b/app.go index 14730b3..c5ff05a 100644 --- a/app.go +++ b/app.go @@ -57,16 +57,20 @@ func (a *App) Run(arguments []string) error { return err } - checkHelp(context) - checkVersion(context) + if checkHelp(context) { + return nil + } + + if checkVersion(context) { + return nil + } args := context.Args() if len(args) > 0 { name := args[0] c := a.Command(name) if c != nil { - c.Run(context) - return nil + return c.Run(context) } } diff --git a/command.go b/command.go index c26ba2a..03f1c11 100644 --- a/command.go +++ b/command.go @@ -3,7 +3,6 @@ package cli import ( "fmt" "io/ioutil" - "os" "strings" ) @@ -24,7 +23,7 @@ type Command struct { } // 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 c.Flags = append( c.Flags, @@ -55,12 +54,15 @@ func (c Command) Run(ctx *Context) { fmt.Println("Incorrect Usage.\n") ShowCommandHelp(ctx, c.Name) fmt.Println("") - os.Exit(1) + return err } context := NewContext(ctx.App, set, ctx.globalSet) - checkCommandHelp(context, c.Name) + if checkCommandHelp(context, c.Name) { + return nil + } c.Action(context) + return nil } // Returns true if Command.Name or Command.ShortName matches given name diff --git a/help.go b/help.go index c34d335..31e0611 100644 --- a/help.go +++ b/help.go @@ -73,7 +73,6 @@ func ShowCommandHelp(c *Context, command string) { } fmt.Printf("No help topic for '%v'\n", command) - os.Exit(1) } // Prints the version number of the App @@ -88,23 +87,29 @@ func printHelp(templ string, data interface{}) { w.Flush() } -func checkVersion(c *Context) { +func checkVersion(c *Context) bool { if c.GlobalBool("version") { 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") { 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") { ShowCommandHelp(c, name) - os.Exit(0) + return true } + + return false }