Fix --version help with CommandDisplayNameAnnotation

When setting Command.Version, a --version option is added. The help
message for the --version command did not consider the command display
name:

    Flags:
      -h, --help      help for kubectl plugin
      -v, --version   version for kubectl-plugin

With this change the help test is consistent with other flags:

    Flags:
      -h, --help      help for kubectl plugin
      -v, --version   version for kubectl plugin
This commit is contained in:
Nir Soffer 2024-08-24 06:00:05 +03:00
parent 511af59cb3
commit 116759b688
2 changed files with 9 additions and 5 deletions

View file

@ -1216,7 +1216,7 @@ func (c *Command) InitDefaultVersionFlag() {
if c.Name() == "" { if c.Name() == "" {
usage += "this command" usage += "this command"
} else { } else {
usage += c.Name() usage += c.displayName()
} }
if c.Flags().ShorthandLookup("v") == nil { if c.Flags().ShorthandLookup("v") == nil {
c.Flags().BoolP("version", "v", false, usage) c.Flags().BoolP("version", "v", false, usage)

View file

@ -371,8 +371,9 @@ func TestAliasPrefixMatching(t *testing.T) {
// text should reflect the way we run the command. // text should reflect the way we run the command.
func TestPlugin(t *testing.T) { func TestPlugin(t *testing.T) {
cmd := &Command{ cmd := &Command{
Use: "kubectl-plugin", Use: "kubectl-plugin",
Args: NoArgs, Version: "1.0.0",
Args: NoArgs,
Annotations: map[string]string{ Annotations: map[string]string{
CommandDisplayNameAnnotation: "kubectl plugin", CommandDisplayNameAnnotation: "kubectl plugin",
}, },
@ -386,13 +387,15 @@ func TestPlugin(t *testing.T) {
checkStringContains(t, cmdHelp, "kubectl plugin [flags]") checkStringContains(t, cmdHelp, "kubectl plugin [flags]")
checkStringContains(t, cmdHelp, "help for kubectl plugin") checkStringContains(t, cmdHelp, "help for kubectl plugin")
checkStringContains(t, cmdHelp, "version for kubectl plugin")
} }
// TestPlugin checks usage as plugin with sub commands. // TestPlugin checks usage as plugin with sub commands.
func TestPluginWithSubCommands(t *testing.T) { func TestPluginWithSubCommands(t *testing.T) {
rootCmd := &Command{ rootCmd := &Command{
Use: "kubectl-plugin", Use: "kubectl-plugin",
Args: NoArgs, Version: "1.0.0",
Args: NoArgs,
Annotations: map[string]string{ Annotations: map[string]string{
CommandDisplayNameAnnotation: "kubectl plugin", CommandDisplayNameAnnotation: "kubectl plugin",
}, },
@ -408,6 +411,7 @@ func TestPluginWithSubCommands(t *testing.T) {
checkStringContains(t, rootHelp, "kubectl plugin [command]") checkStringContains(t, rootHelp, "kubectl plugin [command]")
checkStringContains(t, rootHelp, "help for kubectl plugin") checkStringContains(t, rootHelp, "help for kubectl plugin")
checkStringContains(t, rootHelp, "version for kubectl plugin")
checkStringContains(t, rootHelp, "kubectl plugin [command] --help") checkStringContains(t, rootHelp, "kubectl plugin [command] --help")
childHelp, err := executeCommand(rootCmd, "sub", "-h") childHelp, err := executeCommand(rootCmd, "sub", "-h")