-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
303 lines (268 loc) · 10 KB
/
cmd.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
package arg
import (
"sort"
)
// Cmd represents a command that will be executed by a Parser.
// The command's Exec method is executed if a Parser can match
// the command's prefix, name, options, and operands.
type Cmd struct {
// The command's prpefix. The prefix is the string that comes
//before the command name.
Prefix string
// The command's name. The name must be unique per prefix.
// If two commands with the same prefix and name are added
// to a Parser object, the later will replace the the earlier.
Name string
// The command's description. The description is used to render
// the command's help.
Description string
// The function that gets executed if the arguments match the command.
Exec func(ExecArgs) error
operands map[int]*operand
shortOpts map[rune]*option
longOpts map[string]*option
hasOpts bool
hasOper bool
}
// Option adds an option that does not require a parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) Option(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description})
}
// ReqString adds a required option with a string parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqString(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
required: true, dataType: typeString})
}
// ReqBool adds a required option with a bool parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqBool(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
required: true, dataType: typeBool})
}
// ReqInt64 adds a required option with a int64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqInt64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
required: true, dataType: typeInt64})
}
// ReqUint64 adds a required option with a uint64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqUint64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
required: true, dataType: typeUint64})
}
// ReqFloat64 adds a required option with a float64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqFloat64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
required: true, dataType: typeFloat64})
}
// ReqEnum adds a required enum option. Valid are the valid values
// that are acceped by this option.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) ReqEnum(short rune, long string, valid []string, description string) {
c.addOption(&option{short: short, long: long, description: description, valid: valid,
required: true, dataType: typeEnum})
}
// OptString adds a optional option with a string parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptString(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
dataType: typeString})
}
// OptBool adds a optional option with a bool parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptBool(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
dataType: typeBool})
}
// OptInt64 adds a optional option with a int64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptInt64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
dataType: typeInt64})
}
// OptUint64 adds a optional option with a uint64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptUint64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
dataType: typeUint64})
}
// OptFloat64 adds a optional option with a float64 parameter.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptFloat64(short rune, long string, description string) {
c.addOption(&option{short: short, long: long, description: description,
dataType: typeFloat64})
}
// OptEnum adds a optinal enum option. Valid are the valid values
// that are acceped by this option.
// Short is a one letter option. Short is not used if '0'.
// Long is the long name for the option. Long is not used if empty.
// The command's description used to render the command's help.
func (c *Cmd) OptEnum(short rune, long string, valid []string, description string) {
c.addOption(&option{short: short, long: long, description: description, valid: valid,
dataType: typeEnum})
}
// StringOperand adds a string operand with the give position and name.
// Position starts from zero on wards. The operand is named by name.
func (c *Cmd) StringOperand(position int, name string) {
c.addOperand(&operand{position: position, name: name, dataType: typeString})
c.hasOper = true
}
// BoolOperand adds a bool operand with the give position and name.
// Position starts from zero on wards. The operand is named by name.
func (c *Cmd) BoolOperand(position int, name string) {
c.addOperand(&operand{position: position, name: name, dataType: typeBool})
c.hasOper = true
}
// Int64Operand adds a int64 operand with the give position and name.
// Position starts from zero on wards. The operand is named by name.
func (c *Cmd) Int64Operand(position int, name string) {
c.addOperand(&operand{position: position, name: name, dataType: typeInt64})
c.hasOper = true
}
// Uint64Operand adds a uint64 operand with the give position and name.
// Position starts from zero on wards. The operand is named by name.
func (c *Cmd) Uint64Operand(position int, name string) {
c.addOperand(&operand{position: position, name: name, dataType: typeUint64})
c.hasOper = true
}
// Float64Operand adds a float64 operand with the give position and name.
// Position starts from zero on wards. The operand is named by name.
func (c *Cmd) Float64Operand(position int, name string) {
c.addOperand(&operand{position: position, name: name, dataType: typeFloat64})
c.hasOper = true
}
func (c *Cmd) addOption(o *option) {
if c.shortOpts == nil {
c.longOpts = make(map[string]*option)
c.shortOpts = make(map[rune]*option)
}
c.hasOpts = true
//We will only add them to the command's
//options if they are not zero value
if o.short != 0 {
c.shortOpts[o.short] = o
}
if o.long != "" {
c.longOpts[o.long] = o
}
}
func (c *Cmd) longOption(name string) (*option, bool) {
if o, ok := c.longOpts[name]; ok {
return o, true
}
return nil, false
}
func (c *Cmd) shortOption(name rune) (*option, bool) {
o, ok := c.shortOpts[name]
return o, ok
}
func (c *Cmd) getRequiredNames() map[string]struct{} {
names := make(map[string]struct{})
for longName, opt := range c.longOpts {
if opt.required {
names[longName] = struct{}{}
}
}
for shortRune, opt := range c.shortOpts {
if opt.required {
names[string(shortRune)] = struct{}{}
}
}
return names
}
func (c *Cmd) longOptions() map[string]*option {
return c.longOpts
}
func (c *Cmd) sortedShortOptions() []*option {
var runes []rune
var names []string
var opts []*option
for _, name := range c.shortOpts {
names = append(names, string(name.short))
}
sort.Strings(names)
for _, name := range names {
rs := getRunes(name)
if len(rs) > 0 && rs[0] != 0 {
runes = append(runes, rs[0])
}
}
for _, short := range runes {
if opt, ok := c.shortOpts[short]; ok {
opts = append(opts, opt)
}
}
return opts
}
func (c *Cmd) sortedOperands() []*operand {
var pos []int
var ops []*operand
for p := range c.operands {
pos = append(pos, p)
}
sort.Ints(pos)
for p := range pos {
if op, ok := c.operands[p]; ok {
ops = append(ops, op)
}
}
return ops
}
func (c *Cmd) addOperand(o *operand) {
if c.operands == nil {
c.operands = make(map[int]*operand)
}
c.operands[o.position] = o
}
func (c *Cmd) getOperands() map[int]operand {
m := make(map[int]operand)
for k, v := range c.operands {
m[k] = *v
}
return m
}
func (c *Cmd) hasOptions() bool {
return c.hasOpts
}
type option struct {
required bool
short rune
long string
description string
dataType dataType
valid []string
arg string
}
type operand struct {
position int
name string
dataType dataType
}