From 116759b688c422e47f5a6ead5d388f31ba4cad39 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sat, 24 Aug 2024 06:00:05 +0300 Subject: [PATCH] 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 --- command.go | 2 +- command_test.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index 2df6975f..a8bda03b 100644 --- a/command.go +++ b/command.go @@ -1216,7 +1216,7 @@ func (c *Command) InitDefaultVersionFlag() { if c.Name() == "" { usage += "this command" } else { - usage += c.Name() + usage += c.displayName() } if c.Flags().ShorthandLookup("v") == nil { c.Flags().BoolP("version", "v", false, usage) diff --git a/command_test.go b/command_test.go index 9ce7a529..caf8c121 100644 --- a/command_test.go +++ b/command_test.go @@ -371,8 +371,9 @@ func TestAliasPrefixMatching(t *testing.T) { // text should reflect the way we run the command. func TestPlugin(t *testing.T) { cmd := &Command{ - Use: "kubectl-plugin", - Args: NoArgs, + Use: "kubectl-plugin", + Version: "1.0.0", + Args: NoArgs, Annotations: map[string]string{ CommandDisplayNameAnnotation: "kubectl plugin", }, @@ -386,13 +387,15 @@ func TestPlugin(t *testing.T) { checkStringContains(t, cmdHelp, "kubectl plugin [flags]") checkStringContains(t, cmdHelp, "help for kubectl plugin") + checkStringContains(t, cmdHelp, "version for kubectl plugin") } // TestPlugin checks usage as plugin with sub commands. func TestPluginWithSubCommands(t *testing.T) { rootCmd := &Command{ - Use: "kubectl-plugin", - Args: NoArgs, + Use: "kubectl-plugin", + Version: "1.0.0", + Args: NoArgs, Annotations: map[string]string{ CommandDisplayNameAnnotation: "kubectl plugin", }, @@ -408,6 +411,7 @@ func TestPluginWithSubCommands(t *testing.T) { checkStringContains(t, rootHelp, "kubectl plugin [command]") checkStringContains(t, rootHelp, "help for kubectl plugin") + checkStringContains(t, rootHelp, "version for kubectl plugin") checkStringContains(t, rootHelp, "kubectl plugin [command] --help") childHelp, err := executeCommand(rootCmd, "sub", "-h")