/
opt_ops.go
executable file
·412 lines (362 loc) · 10.9 KB
/
opt_ops.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright © 2019 Hedzr Yeh.
*/
package cmdr
import "time"
type (
// RootCmdOpt for fluent api
RootCmdOpt struct {
optCommandImpl
}
// cmdOpt for fluent api
cmdOpt struct {
optCommandImpl
}
// subCmdOpt for fluent api
subCmdOpt struct {
optCommandImpl
}
// stringOpt for fluent api
stringOpt struct {
optFlagImpl
}
// stringSliceOpt for fluent api
stringSliceOpt struct {
optFlagImpl
}
// intSliceOpt for fluent api
intSliceOpt struct {
optFlagImpl
}
// uintSliceOpt for fluent api
uintSliceOpt struct {
optFlagImpl
}
// boolOpt for fluent api
boolOpt struct {
optFlagImpl
}
// intOpt for fluent api
intOpt struct {
optFlagImpl
}
// uintOpt for fluent api
uintOpt struct {
optFlagImpl
}
// int64Opt for fluent api
int64Opt struct {
optFlagImpl
}
// uint64Opt for fluent api
uint64Opt struct {
optFlagImpl
}
// float32Opt for fluent api
float32Opt struct {
optFlagImpl
}
// float64Opt for fluent api
float64Opt struct {
optFlagImpl
}
// complex64Opt for fluent api
complex64Opt struct {
optFlagImpl
}
// complex128Opt for fluent api
complex128Opt struct {
optFlagImpl
}
// durationOpt for fluent api
durationOpt struct {
optFlagImpl
}
)
// Header for fluent api
func (s *RootCmdOpt) Header(header string) *RootCmdOpt {
optCtx.root.Header = header
return s
}
// Copyright for fluent api
func (s *RootCmdOpt) Copyright(copyright, author string) *RootCmdOpt {
optCtx.root.Copyright = copyright
optCtx.root.Author = author
return s
}
// RunAsSubCommand set RootCommand.RunAsSubCommand field.
// NOTE that when it's valid, RootCommand.Command.Action handler will be ignored.
func (s *RootCmdOpt) RunAsSubCommand(dottedPathToSubCommand string) *RootCmdOpt {
optCtx.root.RunAsSubCommand = dottedPathToSubCommand
return s
}
// AddGlobalPreAction attaches your pre handler to the global Pre-Actions list
func (s *RootCmdOpt) AddGlobalPreAction(pre Handler) *RootCmdOpt {
optCtx.root.PreActions = append(optCtx.root.PreActions, pre)
return s
}
// AddGlobalPostAction attaches your post handler to the global Post-Actions list
func (s *RootCmdOpt) AddGlobalPostAction(post Invoker) *RootCmdOpt {
optCtx.root.PostActions = append(optCtx.root.PostActions, post)
return s
}
// AppendPostActions adds the global post-action to cmdr system
func (s *RootCmdOpt) AppendPostActions(fns ...func(cmd *Command, args []string)) {
for _, fn := range fns {
optCtx.root.PostActions = append(optCtx.root.PostActions, fn)
}
}
// AppendPreActions adds the global pre-action to cmdr system
func (s *RootCmdOpt) AppendPreActions(fns ...func(cmd *Command, args []string) (err error)) {
for _, fn := range fns {
optCtx.root.PreActions = append(optCtx.root.PreActions, fn)
}
}
// func (s *RootCmdOpt) Command(cmdOpt *cmdOpt) *RootCmdOpt {
// optCtx.root.Command = *cmdOpt.workingFlag
// return s
// }
// func (s *RootCmdOpt) SubCmd() (opt OptCmd) {
// cmd := &Command{}
// optCtx.root.SubCommands = append(optCtx.root.SubCommands, cmd)
// optCtx.current = cmd
// return &subCmdOpt{optCommandImpl: optCommandImpl{workingFlag: cmd},}
// }
// NewCmdFrom creates a wrapped Command object as OptCmd, and make it as the current working item.
func NewCmdFrom(cmd *Command) (opt OptCmd) {
optCtx.current = cmd
return &cmdOpt{optCommandImpl: optCommandImpl{working: optCtx.current}}
}
// NewCmd creates a wrapped Command object as OptCmd
func NewCmd() (opt OptCmd) {
current := &Command{root: optCtx.root}
if optCtx.temp == nil {
optCtx.temp = &Command{}
}
return &cmdOpt{optCommandImpl: optCommandImpl{working: current}}
}
// NewSubCmd creates a wrapped Command object as OptCmd, and append it into the current working item.
func NewSubCmd() (opt OptCmd) {
cmd := &Command{root: optCtx.root}
if optCtx.temp == nil {
optCtx.temp = &Command{}
}
optCtx.temp.SubCommands = uniAddCmd(optCtx.temp.SubCommands, cmd)
return &subCmdOpt{optCommandImpl: optCommandImpl{working: cmd}}
}
func uniAddCmd(cmds []*Command, cmd *Command) []*Command {
for _, f := range cmds {
if f == cmd || f.EqualTo(cmd) {
return cmds
}
}
return append(cmds, cmd)
}
func uniAddFlg(flags []*Flag, flg *Flag) []*Flag {
for _, f := range flags {
if f == flg || f.EqualTo(flg) {
return flags
}
}
return append(flags, flg)
}
func uniAddStr(a []string, s string) []string {
for _, f := range a {
if f == s {
return a
}
}
return append(a, s)
}
func uniAddStrs(a []string, ss ...string) []string {
for _, s := range ss {
found := false
for _, f := range a {
if f == s {
found = true
break
}
}
if !found {
a = append(a, s)
}
}
return a
}
// NewBool creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewBool(defaultValue ...bool) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, workingFlag)
opt = &boolOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv bool
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewString creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewString(defaultValue ...string) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &stringOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv string
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewStringSlice creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
//
// Sample:
//
// cmdr.NewStringSlice("quick", "fox", "jumps").Titles("int-slice", "is").Group("").
// AttachTo(parentCmdOpt)
func NewStringSlice(defaultValue ...string) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &stringSliceOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
opt.DefaultValue(defaultValue, "")
return
}
// NewIntSlice creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
//
// Sample:
//
// cmdr.NewIntSlice(1, 2, 3).Titles("int-slice", "is").Group("").
// AttachTo(parentCmdOpt)
func NewIntSlice(defaultValue ...int) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &intSliceOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
opt.DefaultValue(defaultValue, "")
return
}
// NewInt creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewInt(defaultValue ...int) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &intOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv int
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewUintSlice creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
//
// Sample:
//
// cmdr.NewUintSlice(1, 2, 3).Titles("uint-slice", "us").Group("").
// AttachTo(parentCmdOpt)
func NewUintSlice(defaultValue ...uint) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &uintSliceOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
opt.DefaultValue(defaultValue, "")
return
}
// NewUint creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewUint(defaultValue ...uint) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &uintOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv uint
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewInt64 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewInt64(defaultValue ...int64) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &int64Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv int64
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewUint64 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewUint64(defaultValue ...uint64) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &uint64Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv uint64
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewFloat32 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewFloat32(defaultValue ...float32) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &float32Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv float32
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewFloat64 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewFloat64(defaultValue ...float64) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &float64Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv float64
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewComplex64 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewComplex64(defaultValue ...complex64) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &complex64Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv complex64
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewComplex128 creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewComplex128(defaultValue ...complex128) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &complex128Opt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv complex128
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewDuration creates a wrapped OptFlag, you can connect it to a OptCmd via OptFlag.AttachXXX later.
func NewDuration(defaultValue ...time.Duration) (opt OptFlag) {
workingFlag := &Flag{}
// optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
opt = &durationOpt{optFlagImpl: optFlagImpl{working: workingFlag}}
var dv time.Duration
for _, v := range defaultValue {
dv = v
}
opt.DefaultValue(dv, "")
return
}
// NewDurationFrom creates a wrapped OptFlag, and append it into the current working item.
func NewDurationFrom(flg *Flag) (opt OptFlag) {
optCtx.workingFlag = flg
optCtx.current.Flags = uniAddFlg(optCtx.current.Flags, optCtx.workingFlag)
return &durationOpt{optFlagImpl: optFlagImpl{working: optCtx.workingFlag}}
}