-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
117 lines (102 loc) · 3.08 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmdline
import "strings"
func (cl *CommandLine) Help(err error, appName string, args []string) {
ok := true
if err != nil {
_, ok = err.(*CommandLineError)
}
if ok {
if len(args) > 0 && (args[0] == "help" || args[0] == "--help" || strings.HasSuffix(args[0], "?")) {
// process help switch and filter
filter := ""
if args[0] == "help" || args[0] == "--help" {
if len(args) == 2 {
filter = args[1]
}
} else if len(args) == 1 {
filter = args[0]
}
if strings.HasSuffix(filter, "?") {
filter = filter[0 : len(filter)-1]
if filter == "-" || filter == "--" {
filter = ""
}
}
cl.printCommandsWorker(filter, true)
} else if len(args) > 0 && len(cl.PrimaryCommand(args)) > 0 {
// command line specified a command but had an error; show help for the command
cl.helpPrintBlanklnFirst()
cl.helpPrintln("Syntax error.")
cl.helpPrintBlankln()
cl.helpPrintln("Command Help:")
cl.helpPrintBlankln()
cl.printCommandWorker(cl.PrimaryCommand(args))
cl.helpPrintBlankln()
} else {
// show full help
var options string
if len(cl.globalOptions.values) == 0 {
options = ""
} else if len(cl.commands.values) == 1 {
options = " <options>"
} else {
options = " <global options>"
}
cmdOptions := ""
for _, cmd := range cl.commands.values {
if len(cmd.OptionSpecs.values) > 0 || len(cmd.PrimaryArgSpec.ValueSpecs) > 0 {
cmdOptions = " <options>"
break
}
}
if cmdOptions == options {
cmdOptions = "" // remove redundancy
}
cmdToken := " <command>"
if cl.unnamedCmd != nil {
cmdToken = ""
}
cl.helpPrintln("Usage: " + appName + options + cmdToken + cmdOptions)
cl.helpPrintBlankln()
cl.printCommandsWorker("", true)
helpLen := 0
for _, cmd := range cl.commands.values {
helpLen += 60 // fudge factor for each line
helpLen += len(cmd.PrimaryArgSpec.HelpText)
helpLen += len(cmd.PrimaryArgSpec.String())
for _, optionSpec := range cmd.OptionSpecs.values {
helpLen += 60 // fudge factor for each line
helpLen += len(optionSpec.HelpText)
helpLen += len(optionSpec.String())
}
}
// only explain filter mechanism if the help text starts getting long
if helpLen/60 >= 10 {
// pick the first command's argument for an example
sampleArg := ""
if len(cl.commands.values) > 0 {
for _, cmdName := range cl.commands.order {
cmd := cl.commands.values[cmdName]
sampleArg = cmd.PrimaryArgSpec.Key
break
}
}
cl.helpPrintBlankln()
if sampleArg == "" || sampleArg == "~" {
// unnamed primary arg
cl.helpPrintln("Search help with: " + appName + " --help <filter text>")
} else {
cl.helpPrintln("Search help with " + appName + " --help <filter text>. Example: " + appName + " --help " + sampleArg)
cl.helpPrintln("Or, put a question mark on the end. Example: " + appName + " " + sampleArg + "?")
}
cl.helpPrintBlankln()
}
}
} else {
// processing produced an error
cl.helpPrintln("")
cl.helpPrintln(err.Error())
cl.helpPrintln("")
}
cl.helpRender()
}