From 33845a4d5dbe71c279b588a45789b25f00c05fe5 Mon Sep 17 00:00:00 2001 From: William North Date: Fri, 24 May 2024 09:04:13 +1000 Subject: [PATCH] cmd/docker: Check command exists before raising flag error Signed-off-by: William North --- cmd/docker/docker.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 27eb0263bd..c2c696b19f 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -116,6 +116,15 @@ func setFlagErrorFunc(dockerCli command.Cli, cmd *cobra.Command) { // is called. flagErrorFunc := cmd.FlagErrorFunc() cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + // When TraverseChildren is set to true on a parent command, + // Cobra will parse flags before looking for the appropriate command to run. + // First check the command exists before raising a flag error + args := os.Args[1:] + commandExists := findCommand(cmd, args) + + if !commandExists { + return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", args[0]) + } if err := pluginmanager.AddPluginCommandStubs(dockerCli, cmd.Root()); err != nil { return err }