Repository navigation
Expand file tree
/
Copy pathpositional_next.go
More file actions
414 lines (327 loc) · 9.23 KB
/
Copy pathpositional_next.go
File metadata and controls
414 lines (327 loc) · 9.23 KB
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
413
414
/*
Szerszam argument library: szargs.
Copyright (C) 2024 Leslie Dancsecs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package szargs
import "fmt"
// Next is a helper method to consume the next arg returning an error
// if there are no more args. Otherwise it extracts the first argument
// returning the modified list.
func next(name string, args []string) (string, []string, error) {
if len(args) < 1 {
return "", args, fmt.Errorf(
"%w: %s", ErrMissing, name,
)
}
return args[0], args[1:], nil
}
// NextString removes and returns the next argument from the argument list.
//
// If no arguments remain, an error is registered.
//
// Returns the next argument value as a string.
func (args *Args) NextString(name, desc string) string {
args.RegisterUsage(name, desc)
result, newArgs, err := next(name, args.args)
args.args = newArgs
args.PushErr(err)
return result
}
// NextFloat64 removes and returns the next argument from the argument list,
// parsing it as a 64 bit floating point number.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a float64, an error is registered.
//
// Returns the next argument value parsed as a float64.
func (args *Args) NextFloat64(name, desc string) float64 {
var (
arg string
result float64
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseFloat64(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextFloat32 removes and returns the next argument from the argument list,
// parsing it as a 32 bit floating point number.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a float32, an error is registered.
//
// Returns the next argument value parsed as a float32.
func (args *Args) NextFloat32(name, desc string) float32 {
var (
arg string
result float32
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseFloat32(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextInt64 removes and returns the next argument from the argument list,
// parsing it as a signed 64 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for an int64, an error is registered.
//
// Returns the next argument value parsed as an int64.
func (args *Args) NextInt64(name, desc string) int64 {
var (
arg string
result int64
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseInt64(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextInt32 removes and returns the next argument from the argument list,
// parsing it as a signed 32 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for an int32, an error is registered.
//
// Returns the next argument value parsed as an int32.
func (args *Args) NextInt32(name, desc string) int32 {
var (
arg string
result int32
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseInt32(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextInt16 removes and returns the next argument from the argument list,
// parsing it as a signed 16 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for an int16, an error is registered.
//
// Returns the next argument value parsed as an int16.
func (args *Args) NextInt16(name, desc string) int16 {
var (
arg string
result int16
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseInt16(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextInt8 removes and returns the next argument from the argument list,
// parsing it as a signed 8 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for an int8, an error is registered.
//
// Returns the next argument value parsed as an int8.
func (args *Args) NextInt8(name, desc string) int8 {
var (
arg string
result int8
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseInt8(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextInt removes and returns the next argument from the argument list,
// parsing it as n signed integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for an int, an error is registered.
//
// Returns the next argument value parsed as an int.
func (args *Args) NextInt(name, desc string) int {
var (
arg string
result int
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseInt(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextUint64 removes and returns the next argument from the argument list,
// parsing it as an unsigned 64 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a uint64, an error is registered.
//
// Returns the next argument value parsed as a uint64.
func (args *Args) NextUint64(name, desc string) uint64 {
var (
arg string
result uint64
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseUint64(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextUint32 removes and returns the next argument from the argument list,
// parsing it as an unsigned 32 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a uint32, an error is registered.
//
// Returns the next argument value parsed as a uint32.
func (args *Args) NextUint32(name, desc string) uint32 {
var (
arg string
result uint32
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseUint32(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextUint16 removes and returns the next argument from the argument list,
// parsing it as an unsigned 16 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a uint16, an error is registered.
//
// Returns the next argument value parsed as a uint16.
func (args *Args) NextUint16(name, desc string) uint16 {
var (
arg string
result uint16
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseUint16(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextUint8 removes and returns the next argument from the argument list,
// parsing it as an unsigned 8 bit integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a uint8, an error is registered.
//
// Returns the next argument value parsed as a uint8.
func (args *Args) NextUint8(name, desc string) uint8 {
var (
arg string
result uint8
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseUint8(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextUint removes and returns the next argument from the argument list,
// parsing it as an unsigned integer.
//
// If no arguments remain, or if the value has invalid syntax or is out of
// range for a uint, an error is registered.
//
// Returns the next argument value parsed as a uint.
func (args *Args) NextUint(name, desc string) uint {
var (
arg string
result uint
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseUint(name, arg)
}
args.args = newArgs
args.PushErr(err)
return result
}
// NextOption removes and returns the next argument from the argument list.
// The value must match one of the entries in validOptions.
//
// If no arguments remain, or if the value is not found in validOptions,
// an error is registered.
//
// Returns the next argument value.
func (args *Args) NextOption(
name string, validOptions []string, desc string,
) string {
var (
arg string
result string
err error
)
args.RegisterUsage(name, desc)
arg, newArgs, err := next(name, args.args)
if err == nil {
result, err = parseOption(name, arg, validOptions)
}
args.args = newArgs
args.PushErr(err)
return result
}