Skip to content
Myr

Fix help text for runnable plugin command

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When creating a plugin without sub commands, the help text included the command name (kubectl-plugin) instead of the display name (kubectl plugin): Usage: kubectl-plugin [flags] The issue is that the usage line for this case does not use the command path but the raw `Use` string, and this case was not tested. Add a test for this case and fix UsageLine() to replace the command name with the display name. Tested using https://github.com/nirs/kubernetes/tree/sample-cli-plugin-help

authored by Nir Soffer · committed by Marc Khouzam · parent df547f5 · Browse files · 2 files+23 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−2 removed

  • other:+23 added−2 removed

2 files+23 added−2 removed

modified:command_test.go+20 added
368 unmodified lines
369// TestPlugin checks usage as plugin for another command such as kubectl. The370// executable is `kubectl-plugin`, but we run it as `kubectl plugin`. The help371// text should reflect the way we run the command.372func TestPlugin(t *testing.T) {373 cmd := &Command{374 Use: "kubectl-plugin",375 Args: NoArgs,376 Annotations: map[string]string{377 CommandDisplayNameAnnotation: "kubectl plugin",378 },379 Run: emptyRun,380 }381382 cmdHelp, err := executeCommand(cmd, "-h")383 if err != nil {384 t.Errorf("Unexpected error: %v", err)385 }386387 checkStringContains(t, cmdHelp, "kubectl plugin [flags]")388 checkStringContains(t, cmdHelp, "help for kubectl plugin")389}390391// TestPlugin checks usage as plugin with sub commands.392func TestPluginWithSubCommands(t *testing.T) {393 rootCmd := &Command{394 Use: "kubectl-plugin",395 Args: NoArgs,396 Annotations: map[string]string{
2423 unmodified lines
modified:command.go+3 added-2 removed
1439 unmodified lines
14401441// UseLine puts out the full usage for a given command (including parents).1442func (c *Command) UseLine() string {1443 var useline string1444 use := strings.Replace(c.Use, c.Name(), c.displayName(), 1)1445 if c.HasParent() {1445 useline = c.parent.CommandPath() + " " + c.Use1446 useline = c.parent.CommandPath() + " " + use1447 } else {1447 useline = c.Use1448 useline = use1449 }1450 if c.DisableFlagsInUseLine {1451 return useline1452 }
439 unmodified lines