diff --git a/cli-plugins/manager/cobra.go b/cli-plugins/manager/cobra.go index feff8a8fd6..9d09018001 100644 --- a/cli-plugins/manager/cobra.go +++ b/cli-plugins/manager/cobra.go @@ -82,7 +82,7 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e cmd.HelpFunc()(rootCmd, args) return nil } - return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", cmd.Name()) + return fmt.Errorf("docker: unknown command: docker %s\n\nRun 'docker --help' for more information", cmd.Name()) }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // Delegate completion to plugin diff --git a/cli/required.go b/cli/required.go index 454e247613..ba4e6e8efa 100644 --- a/cli/required.go +++ b/cli/required.go @@ -1,8 +1,6 @@ package cli import ( - "strings" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -14,7 +12,13 @@ func NoArgs(cmd *cobra.Command, args []string) error { } if cmd.HasSubCommands() { - return errors.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n")) + return errors.Errorf( + "%[1]s: unknown command: %[2]s %[3]s\n\nUsage: %[4]s\n\nRun '%[2]s --help' for more information", + binName(cmd), + cmd.CommandPath(), + args[0], + cmd.UseLine(), + ) } return errors.Errorf( @@ -99,6 +103,11 @@ func ExactArgs(number int) cobra.PositionalArgs { } } +// binName returns the name of the binary / root command (usually 'docker'). +func binName(cmd *cobra.Command) string { + return cmd.Root().Name() +} + //nolint:unparam func pluralize(word string, number int) string { if number == 1 { diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 7825c7d354..ca732180f1 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -84,7 +84,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { if len(args) == 0 { return command.ShowHelp(dockerCli.Err())(cmd, args) } - return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", args[0]) + return fmt.Errorf("docker: unknown command: docker %s\n\nRun 'docker --help' for more information", args[0]) }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { return isSupported(cmd, dockerCli) diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 8fc7ade3a4..84ea272a20 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -66,7 +66,7 @@ func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { func TestExitStatusForInvalidSubcommand(t *testing.T) { err := runCliCommand(t, nil, nil, "invalid") - assert.Check(t, is.ErrorContains(err, "docker: 'invalid' is not a docker command.")) + assert.Check(t, is.ErrorContains(err, "docker: 'docker invalid' is not a docker command.")) } func TestVersion(t *testing.T) { diff --git a/e2e/cli-plugins/testdata/docker-badmeta-err.golden b/e2e/cli-plugins/testdata/docker-badmeta-err.golden index df2344638a..9342bdaf55 100644 --- a/e2e/cli-plugins/testdata/docker-badmeta-err.golden +++ b/e2e/cli-plugins/testdata/docker-badmeta-err.golden @@ -1,2 +1,3 @@ -docker: 'badmeta' is not a docker command. -See 'docker --help' +docker: unknown command: docker badmeta + +Run 'docker --help' for more information diff --git a/e2e/cli-plugins/testdata/docker-nonexistent-err.golden b/e2e/cli-plugins/testdata/docker-nonexistent-err.golden index f2265f6b9e..12c26f5f00 100644 --- a/e2e/cli-plugins/testdata/docker-nonexistent-err.golden +++ b/e2e/cli-plugins/testdata/docker-nonexistent-err.golden @@ -1,2 +1,3 @@ -docker: 'nonexistent' is not a docker command. -See 'docker --help' +docker: unknown command: docker nonexistent + +Run 'docker --help' for more information