Merge pull request #1787 from ijc/cli-plugins-help-options

cli-plugins: Reinstate deprecated `-h` short form of `--help`.
This commit is contained in:
Sebastiaan van Stijn 2019-03-29 17:13:42 +01:00 committed by GitHub
commit 51235e8253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 21 deletions

View File

@ -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
}

View File

@ -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)
@ -17,8 +19,8 @@ func TestRunNonexisting(t *testing.T) {
res := icmd.RunCmd(run("nonexistent"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-nonexistent-err.golden")
}
@ -30,8 +32,8 @@ func TestHelpNonexisting(t *testing.T) {
res := icmd.RunCmd(run("help", "nonexistent"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-help-nonexistent-err.golden")
}
@ -48,6 +50,16 @@ func TestNonexistingHelp(t *testing.T) {
// output, so spot check instead having of a golden
// with everything in, which will change too frequently.
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,
})
}
@ -59,8 +71,8 @@ func TestRunBad(t *testing.T) {
res := icmd.RunCmd(run("badmeta"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-badmeta-err.golden")
}
@ -72,8 +84,8 @@ func TestHelpBad(t *testing.T) {
res := icmd.RunCmd(run("help", "badmeta"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-help-badmeta-err.golden")
}
@ -90,6 +102,16 @@ func TestBadHelp(t *testing.T) {
// output, so spot check instead of a golden with
// everything in which will change all the time.
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,
})
}
@ -102,6 +124,7 @@ func TestRunGood(t *testing.T) {
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "Hello World!",
Err: icmd.None,
})
}
@ -113,9 +136,11 @@ func TestHelpGood(t *testing.T) {
defer cleanup()
res := icmd.RunCmd(run("-D", "help", "helloworld"))
res.Assert(t, icmd.Success)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Err: icmd.None,
})
golden.Assert(t, res.Stdout(), "docker-help-helloworld.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
}
// TestGoodHelp ensures correct behaviour when calling a valid plugin
@ -126,10 +151,21 @@ func TestGoodHelp(t *testing.T) {
defer cleanup()
res := icmd.RunCmd(run("-D", "helloworld", "--help"))
res.Assert(t, icmd.Success)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Err: icmd.None,
})
// This is the same golden file as `TestHelpGood`, above.
golden.Assert(t, res.Stdout(), "docker-help-helloworld.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
// 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
@ -141,6 +177,7 @@ func TestRunGoodSubcommand(t *testing.T) {
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "Goodbye World!",
Err: icmd.None,
})
}
@ -152,9 +189,11 @@ func TestHelpGoodSubcommand(t *testing.T) {
defer cleanup()
res := icmd.RunCmd(run("-D", "help", "helloworld", "goodbye"))
res.Assert(t, icmd.Success)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Err: icmd.None,
})
golden.Assert(t, res.Stdout(), "docker-help-helloworld-goodbye.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
}
// TestGoodSubcommandHelp ensures correct behaviour when calling a valid plugin
@ -165,10 +204,12 @@ func TestGoodSubcommandHelp(t *testing.T) {
defer cleanup()
res := icmd.RunCmd(run("-D", "helloworld", "goodbye", "--help"))
res.Assert(t, icmd.Success)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Err: icmd.None,
})
// This is the same golden file as `TestHelpGoodSubcommand`, above.
golden.Assert(t, res.Stdout(), "docker-help-helloworld-goodbye.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
}
// TestCliInitialized tests the code paths which ensure that the Cli
@ -191,7 +232,7 @@ func TestPluginErrorCode(t *testing.T) {
res := icmd.RunCmd(run("helloworld", "exitstatus2"))
res.Assert(t, icmd.Expected{
ExitCode: 2,
Out: icmd.None,
Err: "Exiting with error status 2",
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
}