mirror of
https://github.com/spf13/cobra
synced 2024-11-04 21:07:19 +00:00
Better handling of parsing args to commands, especially the root
This commit is contained in:
parent
2d02c310f3
commit
fb5077acbe
1 changed files with 17 additions and 1 deletions
18
commander.go
18
commander.go
|
@ -79,10 +79,26 @@ func (c *Commander) Execute() (err error) {
|
||||||
// overriding
|
// overriding
|
||||||
c.initHelp()
|
c.initHelp()
|
||||||
if len(c.args) == 0 {
|
if len(c.args) == 0 {
|
||||||
err = c.execute(os.Args[1:])
|
if len(os.Args) == 1 {
|
||||||
|
// If only the executable is called and the root is runnable, run it
|
||||||
|
if c.Runnable() {
|
||||||
|
argWoFlags := c.Flags().Args()
|
||||||
|
c.Run(c.cmd, argWoFlags)
|
||||||
|
} else {
|
||||||
|
c.Usage()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = c.execute(os.Args[1:])
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = c.execute(c.args)
|
err = c.execute(c.args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.Println("Error:", err.Error())
|
||||||
|
c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:])
|
||||||
|
c.Printf("Run '%v help' for usage\n", c.Root().Name())
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue