diff --git a/cli/cobra.go b/cli/cobra.go index 1385743f4b..ed9c9b5bac 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -42,6 +42,10 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p rootCmd.SetFlagErrorFunc(FlagErrorFunc) rootCmd.SetHelpCommand(helpCommand) + rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage") + rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help") + rootCmd.PersistentFlags().Lookup("help").Hidden = true + return opts, flags, helpCommand } @@ -52,20 +56,12 @@ func SetupRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.F rootCmd.SetVersionTemplate("Docker version {{.Version}}\n") - rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage") - rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help") - rootCmd.PersistentFlags().Lookup("help").Hidden = true - return opts, flags, helpCmd } // SetupPluginRootCommand sets default usage, help and error handling for a plugin root command. func SetupPluginRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { opts, flags, _ := setupCommonRootCommand(rootCmd) - - rootCmd.PersistentFlags().BoolP("help", "", false, "Print usage") - rootCmd.PersistentFlags().Lookup("help").Hidden = true - return opts, flags } diff --git a/e2e/cli-plugins/run_test.go b/e2e/cli-plugins/run_test.go index f337a99d18..1309cc0d58 100644 --- a/e2e/cli-plugins/run_test.go +++ b/e2e/cli-plugins/run_test.go @@ -9,6 +9,8 @@ import ( "gotest.tools/icmd" ) +const shortHFlagDeprecated = "Flag shorthand -h has been deprecated, please use --help\n" + // TestRunNonexisting ensures correct behaviour when running a nonexistent plugin. func TestRunNonexisting(t *testing.T) { run, _, cleanup := prepare(t) @@ -50,6 +52,15 @@ func TestNonexistingHelp(t *testing.T) { Out: "Usage: docker [OPTIONS] COMMAND\n\nA self-sufficient runtime for containers", Err: icmd.None, }) + // Short -h should be the same, modulo the deprecation message + exp := shortHFlagDeprecated + res.Stdout() + res = icmd.RunCmd(run("nonexistent", "-h")) + res.Assert(t, icmd.Expected{ + ExitCode: 0, + // This should be identical to the --help case above + Out: exp, + Err: icmd.None, + }) } // TestRunBad ensures correct behaviour when running an existent but invalid plugin @@ -93,6 +104,15 @@ func TestBadHelp(t *testing.T) { Out: "Usage: docker [OPTIONS] COMMAND\n\nA self-sufficient runtime for containers", Err: icmd.None, }) + // Short -h should be the same, modulo the deprecation message + exp := shortHFlagDeprecated + res.Stdout() + res = icmd.RunCmd(run("badmeta", "-h")) + res.Assert(t, icmd.Expected{ + ExitCode: 0, + // This should be identical to the --help case above + Out: exp, + Err: icmd.None, + }) } // TestRunGood ensures correct behaviour when running a valid plugin @@ -137,6 +157,15 @@ func TestGoodHelp(t *testing.T) { }) // This is the same golden file as `TestHelpGood`, above. golden.Assert(t, res.Stdout(), "docker-help-helloworld.golden") + // Short -h should be the same, modulo the deprecation message + exp := shortHFlagDeprecated + res.Stdout() + res = icmd.RunCmd(run("-D", "helloworld", "-h")) + res.Assert(t, icmd.Expected{ + ExitCode: 0, + // This should be identical to the --help case above + Out: exp, + Err: icmd.None, + }) } // TestRunGoodSubcommand ensures correct behaviour when running a valid plugin with a subcommand