mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Better error handling
This commit is contained in:
parent
36e08ffd5b
commit
a618569eea
1 changed files with 11 additions and 6 deletions
11
cobra.go
11
cobra.go
|
@ -67,12 +67,13 @@ func (c *Commander) SetArgs(a []string) {
|
||||||
c.args = a
|
c.args = a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) Execute() {
|
func (c *Commander) Execute() (err error) {
|
||||||
if len(c.args) == 0 {
|
if len(c.args) == 0 {
|
||||||
c.execute(os.Args[1:])
|
err = c.execute(os.Args[1:])
|
||||||
} else {
|
} else {
|
||||||
c.execute(c.args)
|
err = c.execute(c.args)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command is just that, a command for your application.
|
// Command is just that, a command for your application.
|
||||||
|
@ -137,10 +138,14 @@ func (c *Command) execute(args []string) (err error) {
|
||||||
cmd, a, e := c.Find(args)
|
cmd, a, e := c.Find(args)
|
||||||
if e == nil {
|
if e == nil {
|
||||||
err = cmd.ParseFlags(a)
|
err = cmd.ParseFlags(a)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
argWoFlags := cmd.Flags().Args()
|
argWoFlags := cmd.Flags().Args()
|
||||||
cmd.Run(cmd, argWoFlags)
|
cmd.Run(cmd, argWoFlags)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
err = e
|
err = e
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue