-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
69 lines (64 loc) · 1.98 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cobra
import "fmt"
// HelpFunc returns either the function set by SetHelpFunc for this command
// or a parent, or it returns a function with default help behavior.
//
// HelpFunc 函数返回执行 help 指令的函数,可以被用户自定义或继承实现
func (cmd *Command) HelpFunc() func(*Command, []string) {
if cmd.helpFunc != nil {
return cmd.helpFunc
}
if cmd.parent != nil {
return cmd.parent.HelpFunc()
}
return func(cmd *Command, a []string) {
if cmd.Long != "" {
fmt.Println(cmd.Long)
}
if cmd.Use != "" {
fmt.Println("Usage:")
fmt.Printf(" %s\n\n", cmd.Use)
}
if len(cmd.commands) != 0 {
fmt.Println("Available Commands:")
for _, subCmd := range cmd.commands {
fmt.Printf(" %-10s %s\n", subCmd.Name(), subCmd.Short)
}
fmt.Println("")
fmt.Printf("Use \"%s help [command] \" for more information about a command.\n", cmd.Name())
}
}
}
// initDefaultHelpCmd adds default help command to c.
// It is called automatically by executing the c or by calling help and usage.
// If c already has help command or c has no subcommands, it will do nothing.
func (cmd *Command) initDefaultHelpCmd() {
if len(cmd.commands) == 0 {
return
}
if cmd.helpCommand == nil {
cmd.helpCommand = &Command{
Use: "help [command]",
Short: "Help about any command",
Long: `Help provides help for any command in the application.
Simply type ` + cmd.Name() + ` help [path to command] for full details.`,
Run: func(c *Command, args []string) {
targetCmd, _, e := c.Root().Find(args)
if targetCmd == nil || e != nil {
fmt.Printf("Unknown help topic %#q\n", args)
c.HelpFunc()
} else {
targetCmd.HelpFunc()(targetCmd, []string{})
}
},
}
}
cmd.RemoveCommand(cmd.helpCommand)
cmd.AddCommand(cmd.helpCommand)
}
// SetHelpFunc sets help function. Can be defined by Application.
//
// SetHelpFunc 函数可以让用户自定义 helpFunc 字段
func (cmd *Command) SetHelpFunc(f func(*Command, []string)) {
cmd.helpFunc = f
}