From 2c370cd936b76ac037f550590a7fbc995bc76af0 Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Fri, 3 Apr 2015 01:07:34 -0400 Subject: [PATCH] Fix redundant error for unknown root command --- cobra_test.go | 9 +++++++++ command.go | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 120bc5c7..68e55efd 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -554,6 +554,15 @@ func TestRootNoCommandHelp(t *testing.T) { } } +func TestRootUnknownCommand(t *testing.T) { + r := noRRSetupTest("bogus") + s := "Error: unknown command \"bogus\"\nRun 'cobra-test help' for usage.\n" + + if r.Output != s { + t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output) + } +} + func TestFlagsBeforeCommand(t *testing.T) { // short without space x := fullSetupTest("-i10 echo") diff --git a/command.go b/command.go index a7d90886..5e882d03 100644 --- a/command.go +++ b/command.go @@ -378,7 +378,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) { // if commander returned and the first argument (if it exists) doesn't // match the command name, return nil & error if commandFound.Name() == c.Name() && len(arrs[0]) > 0 && commandFound.Name() != arrs[0] { - return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0]) + return nil, a, fmt.Errorf("unknown command %q", a[0]) } return commandFound, a, nil @@ -539,10 +539,10 @@ func (c *Command) Execute() (err error) { if err != nil { if err == flag.ErrHelp { c.Help() + } else { 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()) + c.Printf("Run '%v help' for usage.\n", c.Root().Name()) } }