From fb86c5c5590c13a0e5d465c29e4c81477161f0a4 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 22 Jun 2015 10:14:37 +0200 Subject: [PATCH] Correctly print the unknown command name by now, if someone calls: `program --validflag unknowncommand` the output will be: ``` Error: unknown command "--validflag" Run 'program help' for usage. ``` This patch strips out flags so the unknown command is printed: ``` Error: unknown command "unknowncommand" Run 'program help' for usage. ``` --- cobra_test.go | 5 +++++ command.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cobra_test.go b/cobra_test.go index 9848d211..7e64907f 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -755,6 +755,11 @@ func TestRootUnknownCommand(t *testing.T) { if r.Output != s { t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output) } + + r = noRRSetupTest("--strtwo=a bogus") + 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) { diff --git a/command.go b/command.go index 3831c337..8375aa56 100644 --- a/command.go +++ b/command.go @@ -424,7 +424,7 @@ func (c *Command) Find(args []string) (*Command, []string, error) { // If we matched on the root, but we asked for a subcommand, return an error if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] { - return nil, a, fmt.Errorf("unknown command %q", a[0]) + return nil, a, fmt.Errorf("unknown command %q", stripFlags(args, c)[0]) } return commandFound, a, nil