From fb5077acbe25857443f84bd636dbfec6994f8ad6 Mon Sep 17 00:00:00 2001 From: spf13 Date: Sun, 29 Sep 2013 02:03:29 -0400 Subject: [PATCH] Better handling of parsing args to commands, especially the root --- commander.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/commander.go b/commander.go index 7f4bedb..2b41528 100644 --- a/commander.go +++ b/commander.go @@ -79,10 +79,26 @@ func (c *Commander) Execute() (err error) { // overriding c.initHelp() 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 { 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 }