diff --git a/command.go b/command.go index f570d1e56..4cd712bcb 100644 --- a/command.go +++ b/command.go @@ -607,7 +607,7 @@ func (c *Command) VersionTemplate() string { if c.HasParent() { return c.parent.VersionTemplate() } - return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}} + return `{{with .DisplayName}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}} ` } diff --git a/command_test.go b/command_test.go index caf8c121a..cd44992bd 100644 --- a/command_test.go +++ b/command_test.go @@ -1094,6 +1094,24 @@ func TestVersionFlagExecuted(t *testing.T) { checkStringContains(t, output, "root version 1.0.0") } +func TestVersionFlagExecutedDiplayName(t *testing.T) { + rootCmd := &Command{ + Use: "kubectl-plugin", + Version: "1.0.0", + Annotations: map[string]string{ + CommandDisplayNameAnnotation: "kubectl plugin", + }, + Run: emptyRun, + } + + output, err := executeCommand(rootCmd, "--version", "arg1") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, "kubectl plugin version 1.0.0") +} + func TestVersionFlagExecutedWithNoName(t *testing.T) { rootCmd := &Command{Version: "1.0.0", Run: emptyRun}