Skip to content
Myr

Fix help text for plugins

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

When using `CommandDisplayNameAnnotation` we want to use it instead of the command name in `--help` message or in the default help command. With current code we get the wrong text in the --help usage text: Flags: -h, --help help for kubectl-plugin And in the long description of the default help command: $ kubectl cobraplugin help -h Help provides help for any command in the application. Simply type kubectl-plugin help [path to command] for full details. The issue was hidden since the test checked only the Usage line. Fixed by extracting a displayName() function and use it when creating FlagSet and when formatting the default help flag usage and the help command long description. Enhance the TestPlugin to check all the lines including the command name.

authored by Nir Soffer · committed by Marc Khouzam · parent e63925d · Browse files · 2 files+28 added−12 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+28 added−12 removed

  • other:+28 added−12 removed

2 files+28 added−12 removed

modified:command_test.go+12 added-1 removed
369 unmodified lines
370// 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 rootCmd := &Command{374 Use: "plugin",374 Use: "kubectl-plugin",375 Args: NoArgs,376 Annotations: map[string]string{377 CommandDisplayNameAnnotation: "kubectl plugin",378 },
7 unmodified lines
386 t.Errorf("Unexpected error: %v", err)387 }388389 checkStringContains(t, rootHelp, "kubectl plugin [command]")390 checkStringContains(t, rootHelp, "help for kubectl plugin")391 checkStringContains(t, rootHelp, "kubectl plugin [command] --help")392393 childHelp, err := executeCommand(rootCmd, "sub", "-h")394 if err != nil {395 t.Errorf("Unexpected error: %v", err)396 }397398 checkStringContains(t, childHelp, "kubectl plugin sub [flags]")399 checkStringContains(t, childHelp, "help for sub")400401 helpHelp, err := executeCommand(rootCmd, "help", "-h")402 if err != nil {403 t.Errorf("Unexpected error: %v", err)404 }405406 checkStringContains(t, helpHelp, "kubectl plugin help [path to command]")407 checkStringContains(t, helpHelp, "kubectl plugin help [command]")408}409410// TestChildSameName checks the correct behaviour of cobra in cases,411// when an application with name "foo" and with subcommand "foo"
2388 unmodified lines
modified:command.go+16 added-11 removed
1185 unmodified lines
1186func (c *Command) InitDefaultHelpFlag() {1187 c.mergePersistentFlags()1188 if c.Flags().Lookup("help") == nil {1189 usage := "help for "1190 if c.Name() == "" {1190 name := c.displayName()1191 if name == "" {1192 usage += "this command"1193 } else {1193 usage += c.Name()1194 usage += name1195 }1196 c.Flags().BoolP("help", "h", false, usage)1197 _ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{"true"})1198 }
37 unmodified lines
1236 c.helpCommand = &Command{1237 Use: "help [command]",1238 Short: "Help about any command",1239 Long: `Help provides help for any command in the application.1239Simply type ` + c.Name() + ` help [path to command] for full details.`,1240Simply type ` + c.displayName() + ` help [path to command] for full details.`,1241 ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1242 var completions []string1243 cmd, _, e := c.Root().Find(args)1244 if e != nil {
182 unmodified lines
1427func (c *Command) CommandPath() string {1428 if c.HasParent() {1429 return c.Parent().CommandPath() + " " + c.Name()1430 }1431 return c.displayName()1432}14331434func (c *Command) displayName() string {1435 if displayName, ok := c.Annotations[CommandDisplayNameAnnotation]; ok {1436 return displayName1437 }1438 return c.Name()
207 unmodified lines
1646// Flags returns the complete FlagSet that applies1647// to this command (local and persistent declared here and by all parents).1648func (c *Command) Flags() *flag.FlagSet {1649 if c.flags == nil {1645 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1650 c.flags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1651 if c.flagErrorBuf == nil {1652 c.flagErrorBuf = new(bytes.Buffer)1653 }1654 c.flags.SetOutput(c.flagErrorBuf)
5 unmodified lines
1660// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.1661func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1662 persistentFlags := c.PersistentFlags()16631659 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1664 out := flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1665 c.LocalFlags().VisitAll(func(f *flag.Flag) {1666 if persistentFlags.Lookup(f.Name) == nil {1667 out.AddFlag(f)1668 }
5 unmodified lines
1674func (c *Command) LocalFlags() *flag.FlagSet {1675 c.mergePersistentFlags()16761677 if c.lflags == nil {1673 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1678 c.lflags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1679 if c.flagErrorBuf == nil {1680 c.flagErrorBuf = new(bytes.Buffer)1681 }1682 c.lflags.SetOutput(c.flagErrorBuf)
18 unmodified lines
1701func (c *Command) InheritedFlags() *flag.FlagSet {1702 c.mergePersistentFlags()17031704 if c.iflags == nil {1700 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1705 c.iflags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1706 if c.flagErrorBuf == nil {1707 c.flagErrorBuf = new(bytes.Buffer)1708 }1709 c.iflags.SetOutput(c.flagErrorBuf)
19 unmodified lines
17291730// PersistentFlags returns the persistent FlagSet specifically set in the current command.1731func (c *Command) PersistentFlags() *flag.FlagSet {1732 if c.pflags == nil {1728 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1733 c.pflags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1734 if c.flagErrorBuf == nil {1735 c.flagErrorBuf = new(bytes.Buffer)1736 }1737 c.pflags.SetOutput(c.flagErrorBuf)
4 unmodified lines
1742// ResetFlags deletes all flags from command.1743func (c *Command) ResetFlags() {1744 c.flagErrorBuf = new(bytes.Buffer)1745 c.flagErrorBuf.Reset()1741 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1746 c.flags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1747 c.flags.SetOutput(c.flagErrorBuf)1743 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1748 c.pflags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1749 c.pflags.SetOutput(c.flagErrorBuf)17501751 c.lflags = nil1752 c.iflags = nil
108 unmodified lines
1861// new persistent flags of all parents.1862// If c.parentsPflags == nil, it makes new.1863func (c *Command) updateParentsPflags() {1864 if c.parentsPflags == nil {1860 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1865 c.parentsPflags = flag.NewFlagSet(c.displayName(), flag.ContinueOnError)1866 c.parentsPflags.SetOutput(c.flagErrorBuf)1867 c.parentsPflags.SortFlags = false1868 }1869
21 unmodified lines