...
1
16
17 package help
18
19 import (
20 "strings"
21
22 "github.com/spf13/cobra"
23
24 "k8s.io/kubectl/pkg/util/i18n"
25 "k8s.io/kubectl/pkg/util/templates"
26 )
27
28 var helpLong = templates.LongDesc(i18n.T(`
29 Help provides help for any command in the application.
30 Simply type kubectl help [path to command] for full details.`))
31
32
33 func NewCmdHelp() *cobra.Command {
34 cmd := &cobra.Command{
35 Use: "help [command] | STRING_TO_SEARCH",
36 DisableFlagsInUseLine: true,
37 Short: i18n.T("Help about any command"),
38 Long: helpLong,
39
40 Run: RunHelp,
41 }
42
43 return cmd
44 }
45
46
47 func RunHelp(cmd *cobra.Command, args []string) {
48 foundCmd, _, err := cmd.Root().Find(args)
49
50
51
52
53 if foundCmd == nil {
54 cmd.Printf("Unknown help topic %#q.\n", args)
55 cmd.Root().Usage()
56 } else if err != nil {
57
58 cmd.Println(err)
59
60 argsString := strings.Join(args, " ")
61 var matchedMsgIsPrinted = false
62 for _, foundCmd := range foundCmd.Commands() {
63 if strings.Contains(foundCmd.Short, argsString) {
64 if !matchedMsgIsPrinted {
65 cmd.Printf("Matchers of string '%s' in short descriptions of commands: \n", argsString)
66 matchedMsgIsPrinted = true
67 }
68 cmd.Printf(" %-14s %s\n", foundCmd.Name(), foundCmd.Short)
69 }
70 }
71
72 if !matchedMsgIsPrinted {
73
74 cmd.Root().Usage()
75 }
76 } else {
77 if len(args) == 0 {
78
79 foundCmd = cmd
80 }
81 helpFunc := foundCmd.HelpFunc()
82 helpFunc(foundCmd, args)
83 }
84 }
85
View as plain text