Merge pull request #83 from adammck/fix_double_output_on_unknown_command

Fix redundant error for unknown root commands
This commit is contained in:
Eric Paris 2015-04-03 15:28:12 -05:00
commit 09b49c329c
2 changed files with 12 additions and 3 deletions

View File

@ -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) { func TestFlagsBeforeCommand(t *testing.T) {
// short without space // short without space
x := fullSetupTest("-i10 echo") x := fullSetupTest("-i10 echo")

View File

@ -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 // if commander returned and the first argument (if it exists) doesn't
// match the command name, return nil & error // match the command name, return nil & error
if commandFound.Name() == c.Name() && len(arrs[0]) > 0 && commandFound.Name() != arrs[0] { 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 return commandFound, a, nil
@ -539,10 +539,10 @@ func (c *Command) Execute() (err error) {
if err != nil { if err != nil {
if err == flag.ErrHelp { if err == flag.ErrHelp {
c.Help() c.Help()
} else { } else {
c.Println("Error:", err.Error()) 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())
} }
} }