-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
163 lines (151 loc) · 3.59 KB
/
cli.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package zcli quickly build cli applications
package zcli
import (
"flag"
"fmt"
"strings"
"github.com/sohaha/zlsgo/zlog"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
)
func init() {
Log = zlog.New()
Log.ResetFlags(zlog.BitLevel)
// flag.CommandLine.SetOutput(ioutil.Discard)
flag.CommandLine.SetOutput(&errWrite{})
flag.Usage = func() {
usage()
}
}
// Add registers a cmd for the provided subCommand name
func Add(name, description string, command Cmd) *cmdCont {
if name == "" {
Log.Error(GetLangText("command_empty"))
return &cmdCont{}
}
cmd := &cmdCont{
name: name,
desc: description,
command: command,
requiredFlags: RequiredFlags{},
}
cmds[name] = cmd
cmdsKey = append(cmdsKey, name)
return cmd
}
// SetUnknownCommand set unknown command handle
func SetUnknownCommand(fn func(_ string)) {
unknownCommandFn = fn
}
func usage() {
showHeadr()
showFlagsAndRequired := func() {
if numOfGlobalFlags() > 0 {
ShowFlags(flag.CommandLine)
ShowRequired(flag.CommandLine, requiredFlags)
}
}
if len(cmds) == 0 {
Log.Printf("usage of %s\n", showText(FirstParameter))
showFlagsAndRequired()
return
}
Log.Printf("usage: %s <command>\n\n", FirstParameter)
Log.Println("where <command> is one of:")
for _, name := range cmdsKey {
if cont, ok := cmds[name]; ok {
// for name, cont := range cmds {
Log.Printf(" "+tipText("%-19s")+" %s", name, cont.desc)
}
}
showFlagsAndRequired()
if !HidePrompt {
Log.Printf(showText("\nMore Command information, please use: %s <command> --help\n"), FirstParameter)
}
}
// ShowFlags ShowFlags
func ShowFlags(fg *flag.FlagSet) {
Log.Printf("\noptional flags:")
max := 40
showFlagsHelp()
flagsItems := zstring.Buffer()
fg.VisitAll(func(f *flag.Flag) {
s := zstring.Buffer()
flagsTitle := strings.Replace(f.Name, cliPrefix, "", 1)
for _, key := range varShortsKey {
if flagsTitle == key {
return
}
}
output := false
if flagsTitle == "version" {
output = true
}
name, usage := flag.UnquoteUsage(f)
for key, v := range varsKey {
shorts := v.shorts
if key == flagsTitle && len(shorts) > 0 {
for key := range shorts {
shorts[key] = "-" + shorts[key]
}
flagsTitle += ", " + strings.Join(shorts, ", ")
}
}
// if name == "" {
// name = "bool"
// }
sf := " -%-12s"
if len(name) > 0 {
newName := showText("<" + name + ">")
namePadLen := 12 + len(newName) - len(name)
flagsTitle += " " + newName
sf = " -%-" + ztype.ToString(namePadLen) + "s"
}
s.WriteString(warnText(fmt.Sprintf(sf, flagsTitle)))
if zstring.Len(s.String()) <= max {
s.WriteString("\t")
} else {
s.WriteString("\n \t")
}
s.WriteString(strings.Replace(usage, "\n", "\n \t", -1))
defValue := ztype.ToString(f.DefValue)
if defValue != "" && defValue != "0" && defValue != "false" {
s.WriteString(fmt.Sprintf(" (default %v)", defValue))
}
if output {
Log.Println(s.String())
} else {
s.WriteString("\n")
flagsItems.WriteString(s.String())
}
})
Log.Println(flagsItems.String())
}
// Start Start
func Start(runFunc ...runFunc) {
if matchingCmd != nil {
if *flagHelp {
showSubcommandUsage(flag.CommandLine, matchingCmd)
} else {
matchingCmd.command.Run(args)
}
return
}
requiredErr := parseRequiredFlags(flag.CommandLine, requiredFlags)
if requiredErr != nil {
Error(requiredErr.Error())
}
isRunFunc := len(runFunc) > 0
if isRunFunc {
runFunc[0]()
} else {
Help()
}
}
// Run runnable
func Run(runFunc ...runFunc) (ok bool) {
isRunFunc := len(runFunc) > 0
parse(!isRunFunc)
Start(runFunc...)
return
}