-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
318 lines (285 loc) · 9.73 KB
/
args.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
package opt
import (
"fmt"
"sort"
"strconv"
"strings"
)
// The flagMap a special type that stores the names of flags and
// their number declared in the data structure.
type flagMap map[string]int
// The argValue is a special type to save the value of the argument.
type argValue struct {
order int // order of the argument on the command line
value string // value for the argument
}
// The argMap a special type of saving arguments in the form of a map.
//
// Save the list of values in the map not as []string but as []argValue
// so that you can reproduce the order of the elements in the list.
// For example, command line as: --users=John -UBob -U Roy, where -U is
// synonym for --users, can be passed as a slice [Bob Roy John] - i.e.
// not maintaining the order that was in the command line.
type argMap map[string][]argValue
// The parse converts the args slice to an argMap type.
//
// The shortFlags is a map of short flags which is used for
// separation value from the flag. Problem, for example: -dUGoloop,
// here -d and -U are short flags and Goloop is value for -U flag.
// How to understand that G is not another flag but the beginning
// of the value? This slice stores all available short flags
// declared in the data structure.
//
// The longFlags is a map of long flags. Problem, for example,
// the flag --no-verbose - is it the no-verbose flag or the objection
// for the verbose flag? This slice stores all available long flags
// declared in the data structure.
func (am argMap) parse(args []string, flags map[string]int) error {
// Controls of parsing state of positional arguments.
posState := struct {
order int // real index of the positional argument
active bool // true if switch to parsing of positional arguments
}{}
// Remove all keys from the map that could be there
// from the previous parsing. We can't use am = make(argMap) here.
for key := range am {
delete(am, key)
}
// Parse all the input arguments. Read elements on an index
// instead of by means of range as sometimes it is necessary
// to take the following argument in the course of current
// iteration and to skip next one iteration.
for i := 0; i < len(args); i++ {
item := args[i]
switch {
case item == "--":
// Toggles the parser to read positional arguments.
//
// Example:
// ./app 5 -UGoloop --verbose false -d -- 10 15
//
// where 5, 10, 15 is positional arguments
// and 10 isn't a value for -d flag.
posState.active = true
case strings.HasPrefix(item, "--") && !posState.active:
// Long flag. The flag for a boolean value may have the no- prefix.
// The value can be specified after the = symbol or as a series of
// subsequent entries in the list. Values may also be missing for
// boolean values.
//
// Example:
// ./app --user Goloop --no-verbose
// ./app --user=Goloop --verbose=false
// ./app --user Goloop --verbose false
// ./app --user Goloop
//
// where --user == "Goloop" and --verbose == false.
var flag, data string
// Separate the flags from the value, like:
// Example: --user=Goloop or --user Goloop where is user is a flag
// and Goloop is a value. Or --no-verbose where verbose is a flag
// and no- is boolean "no".
// Split data and flag name.
// Write the name of the long flag in lower case,
// but we don't change the case for value.
flag = strings.TrimPrefix(item, "--")
if tmp := strings.SplitN(flag, "=", 2); len(tmp) == 2 {
flag, data = strings.ToLower(tmp[0]), tmp[1]
} else {
flag = strings.ToLower(flag)
}
// Detect reverse mode and flag availability.
switch _, ok := flags[flag]; {
case !ok && strings.HasPrefix(flag, "no-"):
// The flag is not available, but there is
// a possibility that it needs reverse mode.
fwn := strings.TrimPrefix(flag, "no-")
if _, ok := flags[fwn]; ok {
// The flag needs reverse mode but cannot be
// reverse mode at the same time as data exists.
if data == "" {
flag, data = fwn, "false"
break
}
}
fallthrough
case !ok:
return fmt.Errorf("invalid argument %s", item)
}
key, value := flag, "true"
if data != "" {
value = string(data)
} else if i+1 < len(args) {
// Try to take the value from the next item.
if tmp := args[i+1]; !strings.HasPrefix(tmp, "-") {
value = tmp
i++ // be sure to move to the right by one position
}
}
am[key] = append(am[key], argValue{i, value})
case strings.HasPrefix(item, "-") && !posState.active:
// Short flag. The short flags can be grouped. The value can be
// concatenated to the flag or as a series of subsequent entries
// in the list.
//
// Example:
// ./app -dUGoloop -g"Hello, world"
// ./app -d -UGoloop -g "Hello, world"
// ./app -U Goloop -dg "Hello, world"
//
// where -d == true, -U == "Goloop" and -g == "Hello, world"
var group, data []rune
// Separate the flags from the value, like:
// Example: -dUGoloop or -dU Goloop where is dU is the group of
// flags and Goloop is value for -U, it is possible when d and U
// in shortFlags and G isn't.
//
// In one group, a repeating flag is considered the result
// of the previous flag. For example: -dUd, this is an alternative
// to -dU d or -d -U d where last d is value for -U. Therefore, it
// is necessary to monitor the uniqueness of the flag in the group
// during parsing.
unique := make(map[rune]bool)
group = []rune(strings.TrimPrefix(item, "-"))
for i, c := range group {
_, exists := unique[c]
if _, ok := flags[string(c)]; !ok || exists {
group, data = group[:i], group[i:]
break
}
unique[c] = true
}
// If no flag is set, this is a command line error.
if len(group) == 0 {
return fmt.Errorf("invalid argument %s", item)
}
for j, flag := range group {
key, value := string(flag), "true"
if j == len(group)-1 {
// For last flag in flag list only.
if len(data) != 0 {
value = strings.TrimLeft(string(data), " ")
} else if i+1 < len(args) {
// Try to take the value from the next item.
if tmp := args[i+1]; !strings.HasPrefix(tmp, "-") {
value = tmp
i++ // be sure to move to the right by one position
}
}
}
am[key] = append(am[key], argValue{i, value})
}
default:
// Value for the previous flag or positional arguments.
// Positional arguments can be sets: before to the first
// short or long flag; after the value to the last flag;
// after switch as -- symbols.
//
// Example:
// ./app 5 10 15 -dUGoloop --verbose
// ./app 5 10 15 --verbose -dUGoloop
// ./app --verbose -dUGoloop 5 10 15
// ./app -dUGoloop --verbose -- 5 10 15
// ./app 5 10 -dUGoloop --verbose -- 15
//
// where 5, 10, 15 is positional arguments.
am[fmt.Sprint(posState.order)] = []argValue{{i, item}}
posState.order++
}
}
return nil
}
// The asFlat returns argMap as simple map[string][]string.
func (am argMap) asFlat() map[string][]string {
result := make(map[string][]string, len(am))
for key, items := range am {
tmp := make([]string, 0, len(items))
for _, item := range items {
tmp = append(tmp, item.value)
}
result[key] = tmp
}
return result
}
// The posValues returns values of positional arguments
// sorted in the sequence specified in the command line.
func (am argMap) posValues() []string {
// Position argument object for sorting.
type posItem struct {
order int // real index of the positional argument
value []string // value of the posipositionaltion argument
}
// Filter positional arguments only
// (such arguments have a key that can be converted to an integer).
tmp := []posItem{}
for key, items := range am {
if order, err := strconv.Atoi(key); err == nil {
if order == 0 {
continue // name of app
}
value := make([]string, 0, len(items))
for _, item := range items {
value = append(value, item.value)
}
tmp = append(tmp, posItem{order, value})
}
}
// Arrange the items in the order specified in the command line.
sort.Slice(tmp, func(i, j int) bool {
return tmp[i].order < tmp[j].order
})
// Return only the values of these arguments.
result := make([]string, 0, len(tmp))
for _, item := range tmp {
// A positional argument can have only one value, for example:
// ./app one two three "Hello World" five - five positional arguments,
// but each has one meaning without exception:
// "0": []string{"./app"},
// "1": []string{"one"},
// "2": []string{"two"},
// ...,
// "5": []string{"five"}.
//
// if len(item.value) != 1 {
// panic("this event will never happen")
// }
result = append(result, item.value[0])
}
return result
}
// The flagValue returns the value for the specified flag by
// long and/or short name with true as second param.
// Returns defValue with false as second param if the value
// is not found.
func (am argMap) flagValue(
shortFlag,
longFlag,
defValue,
sepList string,
) ([]string, bool) {
var result []string
// Join all values from all types of flags (long/short).
tmp := []argValue{}
for _, key := range []string{shortFlag, longFlag} {
if items, ok := am[key]; ok {
tmp = append(tmp, items...)
}
}
// Return default value.
if len(tmp) == 0 {
return []string{defValue}, false
}
// Be sure to set the sequence of values that was in the command line.
// For example: -UOne --users=Two -U Three where U and users is synonyms,
// result should be ["One", "Two", "Three"], but now it will
// look like ["One", "Three", "Two"] because short flags
// are processed first.
sort.Slice(tmp, func(i, j int) bool {
return tmp[i].order < tmp[j].order
})
// Get the value of the arguments only.
for _, item := range tmp {
result = append(result, item.value)
}
return result, true
}