From a34876d7008f79e4317c2271f7fa4698d03067f7 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 19 Jun 2019 14:08:05 +0200 Subject: [PATCH] Display an error when help is called on a non existing command. --- pkg/cli/commands.go | 10 +++++----- pkg/cli/commands_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index 6e24e2a4b..e2227c12c 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -86,6 +86,11 @@ func execute(cmd *Command, args []string, root bool) error { } func run(cmd *Command, args []string) error { + if len(args) > 0 && !isFlag(args[0]) && !cmd.AllowArg { + _ = PrintHelp(os.Stdout, cmd) + return fmt.Errorf("command not found: %s", args[0]) + } + if isHelp(args) { return PrintHelp(os.Stdout, cmd) } @@ -95,11 +100,6 @@ func run(cmd *Command, args []string) error { return fmt.Errorf("command %s is not runnable", cmd.Name) } - if len(args) > 0 && !isFlag(args[0]) && !cmd.AllowArg { - _ = PrintHelp(os.Stdout, cmd) - return fmt.Errorf("command not found: %v", args) - } - if cmd.Configuration == nil { return cmd.Run(args) } diff --git a/pkg/cli/commands_test.go b/pkg/cli/commands_test.go index 77a6b8329..e4f4a1d92 100644 --- a/pkg/cli/commands_test.go +++ b/pkg/cli/commands_test.go @@ -103,6 +103,22 @@ func Test_execute(t *testing.T) { }, expected: expected{error: true}, }, + { + desc: "root command, call help, with argument, command not found", + args: []string{"", "echo", "--help"}, + command: func() *Command { + return &Command{ + Name: "root", + Description: "This is a test", + Configuration: nil, + Run: func(_ []string) error { + called = "root" + return nil + }, + } + }, + expected: expected{error: true}, + }, { desc: "one sub command", args: []string{"", "sub1"},