generated from miniaturebase/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
58 lines (44 loc) · 1.02 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
package console
import (
"fmt"
"log"
"strings"
)
var help = &Command{
Name: "help",
Description: "Display this message or learn about other commands",
Run: func(command *Command) ExitCode {
argv := command.flags.Args()
argc := len(argv)
if argc == 0 {
if "" != strings.TrimSpace(command.app.header) {
fmt.Print(command.app.header)
}
commands := make([]*Command, len(command.app.Commands))
var index int
for _, subcommand := range command.app.Commands {
commands[index] = subcommand
index++
}
return helpCommand(&Command{
Commands: commands,
})
}
root := argv[0]
parent, exists := command.app.Commands[root]
if !exists {
log.Panicf("Unknown command given '%s'", root)
}
if argc == 1 {
return helpCommand(parent)
}
target := argv[1]
for _, subcommand := range parent.Commands {
if target != subcommand.Name {
continue
}
subcommand.Name = fmt.Sprintf("%s %s", parent.Name, target)
return helpCommand(subcommand)
}
return 0
}}