From a618569eeabc8dcf3d14c1c90d7c6809c6ce1048 Mon Sep 17 00:00:00 2001 From: spf13 Date: Wed, 4 Sep 2013 11:23:13 -0400 Subject: [PATCH] Better error handling --- cobra.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cobra.go b/cobra.go index 44ef4d0c..d28ec40b 100644 --- a/cobra.go +++ b/cobra.go @@ -67,12 +67,13 @@ func (c *Commander) SetArgs(a []string) { c.args = a } -func (c *Commander) Execute() { +func (c *Commander) Execute() (err error) { if len(c.args) == 0 { - c.execute(os.Args[1:]) + err = c.execute(os.Args[1:]) } else { - c.execute(c.args) + err = c.execute(c.args) } + return } // Command is just that, a command for your application. @@ -137,9 +138,13 @@ func (c *Command) execute(args []string) (err error) { cmd, a, e := c.Find(args) if e == nil { err = cmd.ParseFlags(a) - argWoFlags := cmd.Flags().Args() - cmd.Run(cmd, argWoFlags) - return nil + if err != nil { + return err + } else { + argWoFlags := cmd.Flags().Args() + cmd.Run(cmd, argWoFlags) + return nil + } } err = e return err