-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
282 lines (253 loc) · 5.51 KB
/
context.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cli
import (
"fmt"
"os"
"strconv"
"strings"
)
// exit variable for tesing hook
var exit = os.Exit
// Context is a type that is passed through to
// each Handler action in a cli application. Context
// can be used to retrieve context-specific Args and
// parsed command-line options.
type Context struct {
name string
app *App
command *Command
flags []*Flag
commands []*Command
args []string
parent *Context
}
// Name returns app/command full name
func (c *Context) Name() string {
return c.name
}
// Parent returns parent context if exists
func (c *Context) Parent() *Context {
return c.parent
}
// Global returns top context if exists
func (c *Context) Global() *Context {
ctx := c
for {
if ctx.parent == nil {
return ctx
}
ctx = ctx.parent
}
}
// IsSet returns flag is visited in cli args
func (c *Context) IsSet(name string) bool {
f := lookupFlag(c.flags, name)
if f != nil {
return f.visited
}
return false
}
// GetString returns flag value as string
func (c *Context) GetString(name string) string {
f := lookupFlag(c.flags, name)
if f != nil {
return f.GetValue()
}
return ""
}
// GetStringSlice returns flag value as string slice
func (c *Context) GetStringSlice(name string) []string {
f := lookupFlag(c.flags, name)
if f != nil {
return strings.Split(f.GetValue(), ",")
}
return nil
}
// GetBool returns flag value as bool
func (c *Context) GetBool(name string) bool {
f := lookupFlag(c.flags, name)
if f != nil {
b, err := strconv.ParseBool(f.GetValue())
if err == nil {
return b
}
}
return false
}
// GetInt returns flag value as int
func (c *Context) GetInt(name string) int {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseInt(f.GetValue(), 0, 0)
if err == nil {
return int(v)
}
}
return 0
}
// GetInt8 returns flag value as int8
func (c *Context) GetInt8(name string) int8 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseInt(f.GetValue(), 0, 8)
if err == nil {
return int8(v)
}
}
return 0
}
// GetInt16 returns flag value as int16
func (c *Context) GetInt16(name string) int16 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseInt(f.GetValue(), 0, 16)
if err == nil {
return int16(v)
}
}
return 0
}
// GetInt32 returns flag value as int32
func (c *Context) GetInt32(name string) int32 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseInt(f.GetValue(), 0, 32)
if err == nil {
return int32(v)
}
}
return 0
}
// GetInt64 returns flag value as int64
func (c *Context) GetInt64(name string) int64 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseInt(f.GetValue(), 0, 64)
if err == nil {
return int64(v)
}
}
return 0
}
// GetUint returns flag value as uint
func (c *Context) GetUint(name string) uint {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseUint(f.GetValue(), 0, 0)
if err == nil {
return uint(v)
}
}
return 0
}
// GetUint8 returns flag value as uint8
func (c *Context) GetUint8(name string) uint8 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseUint(f.GetValue(), 0, 8)
if err == nil {
return uint8(v)
}
}
return 0
}
// GetUint16 returns flag value as uint16
func (c *Context) GetUint16(name string) uint16 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseUint(f.GetValue(), 0, 16)
if err == nil {
return uint16(v)
}
}
return 0
}
// GetUint32 returns flag value as uint32
func (c *Context) GetUint32(name string) uint32 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseUint(f.GetValue(), 0, 32)
if err == nil {
return uint32(v)
}
}
return 0
}
// GetUint64 returns flag value as uint64
func (c *Context) GetUint64(name string) uint64 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseUint(f.GetValue(), 0, 64)
if err == nil {
return uint64(v)
}
}
return 0
}
// GetFloat32 returns flag value as float32
func (c *Context) GetFloat32(name string) float32 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseFloat(f.GetValue(), 32)
if err == nil {
return float32(v)
}
}
return 0
}
// GetFloat64 returns flag value as float64
func (c *Context) GetFloat64(name string) float64 {
f := lookupFlag(c.flags, name)
if f != nil {
v, err := strconv.ParseFloat(f.GetValue(), 64)
if err == nil {
return float64(v)
}
}
return 0
}
// NArg returns number of non-flag arguments
func (c *Context) NArg() int {
return len(c.args)
}
// Arg returns the i'th non-flag argument
func (c *Context) Arg(n int) string {
return c.args[n]
}
// Args returns the non-flag arguments.
func (c *Context) Args() []string {
return c.args
}
// ShowHelp shows help and
func (c *Context) ShowHelp() {
if c.command != nil {
c.command.ShowHelp(newCommandHelpContext(c.name, c.command, c.app))
} else {
c.app.ShowHelp(newAppHelpContext(c.name, c.app))
}
}
// ShowHelpAndExit shows help and exit
func (c *Context) ShowHelpAndExit(code int) {
c.ShowHelp()
exit(code)
}
// ShowError shows error and exit(1)
func (c *Context) ShowError(err error) {
w := os.Stderr
fmt.Fprintln(w, err)
fmt.Fprintln(w, fmt.Sprintf("\nRun '%s --help' for more information", c.name))
exit(1)
}
func (c *Context) handlePanic() {
if e := recover(); e != nil {
if c.app.OnActionPanic != nil {
err, ok := e.(error)
if !ok {
err = fmt.Errorf("%v", e)
}
c.app.OnActionPanic(c, err)
} else {
os.Stderr.WriteString(fmt.Sprintf("fatal: %v\n", e))
}
exit(1)
}
}