-
Notifications
You must be signed in to change notification settings - Fork 16
/
args.zig
516 lines (436 loc) · 18.1 KB
/
args.zig
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// MIT License
// Copyright (c) 2020 Felix Queißner
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const std = @import("std");
/// Parses arguments for the given specification and our current process.
/// - `Spec` is the configuration of the arguments.
/// - `allocator` is the allocator that is used to allocate all required memory
/// - `error_handling` defines how parser errors will be handled.
pub fn parseForCurrentProcess(comptime Spec: type, allocator: *std.mem.Allocator, error_handling: ErrorHandling) !ParseArgsResult(Spec) {
var args = std.process.args();
const executable_name = try (args.next(allocator) orelse {
try error_handling.process(error.NoExecutableName, Error{
.option = "",
.kind = .missing_executable_name,
});
// we do not assume any more arguments appear here anyways...
return error.NoExecutableName;
});
errdefer allocator.free(executable_name);
var result = try parse(Spec, &args, allocator, error_handling);
result.executable_name = executable_name;
return result;
}
/// Parses arguments for the given specification.
/// - `Spec` is the configuration of the arguments.
/// - `args` is an ArgIterator that will yield the command line arguments.
/// - `allocator` is the allocator that is used to allocate all required memory
/// - `error_handling` defines how parser errors will be handled.
///
/// Note that `.executable_name` in the result will not be set!
pub fn parse(comptime Spec: type, args: *std.process.ArgIterator, allocator: *std.mem.Allocator, error_handling: ErrorHandling) !ParseArgsResult(Spec) {
var result = ParseArgsResult(Spec){
.arena = std.heap.ArenaAllocator.init(allocator),
.options = Spec{},
.positionals = undefined,
.executable_name = null,
};
errdefer result.arena.deinit();
var arglist = std.ArrayList([:0]const u8).init(allocator);
errdefer arglist.deinit();
var last_error: ?anyerror = null;
while (args.next(&result.arena.allocator)) |item_or_error| {
const item = try item_or_error;
if (std.mem.startsWith(u8, item, "--")) {
if (std.mem.eql(u8, item, "--")) {
// double hyphen is considered 'everything from here now is positional'
break;
}
const Pair = struct {
name: []const u8,
value: ?[]const u8,
};
const pair = if (std.mem.indexOf(u8, item, "=")) |index|
Pair{
.name = item[2..index],
.value = item[index + 1 ..],
}
else
Pair{
.name = item[2..],
.value = null,
};
var found = false;
inline for (std.meta.fields(Spec)) |fld| {
if (std.mem.eql(u8, pair.name, fld.name)) {
try parseOption(Spec, &result, args, error_handling, &last_error, fld.name, pair.value);
found = true;
}
}
if (!found) {
last_error = error.EncounteredUnknownArgument;
try error_handling.process(error.EncounteredUnknownArgument, Error{
.option = pair.name,
.kind = .unknown,
});
}
} else if (std.mem.startsWith(u8, item, "-")) {
if (std.mem.eql(u8, item, "-")) {
// single hyphen is considered a positional argument
try arglist.append(item);
} else {
if (@hasDecl(Spec, "shorthands")) {
for (item[1..]) |char, index| {
var option_name = [2]u8{ '-', char };
var found = false;
inline for (std.meta.fields(@TypeOf(Spec.shorthands))) |fld| {
if (fld.name.len != 1)
@compileError("All shorthand fields must be exactly one character long!");
if (fld.name[0] == char) {
const real_name = @field(Spec.shorthands, fld.name);
const real_fld_type = @TypeOf(@field(result.options, real_name));
// -2 because we stripped of the "-" at the beginning
if (requiresArg(real_fld_type) and index != item.len - 2) {
last_error = error.EncounteredUnexpectedArgument;
try error_handling.process(error.EncounteredUnexpectedArgument, Error{
.option = &option_name,
.kind = .invalid_placement,
});
} else {
try parseOption(Spec, &result, args, error_handling, &last_error, real_name, null);
}
found = true;
}
}
if (!found) {
last_error = error.EncounteredUnknownArgument;
try error_handling.process(error.EncounteredUnknownArgument, Error{
.option = &option_name,
.kind = .unknown,
});
}
}
} else {
try error_handling.process(error.EncounteredUnsupportedArgument, Error{
.option = item,
.kind = .unsupported,
});
}
}
} else {
try arglist.append(item);
}
}
if (last_error != null)
return error.InvalidArguments;
// This will consume the rest of the arguments as positional ones.
// Only executes when the above loop is broken.
while (args.next(&result.arena.allocator)) |item_or_error| {
const item = try item_or_error;
try arglist.append(item);
}
result.positionals = arglist.toOwnedSlice();
return result;
}
/// The return type of the argument parser.
pub fn ParseArgsResult(comptime Spec: type) type {
return struct {
const Self = @This();
/// Exports the type of options.
pub const Options = Spec;
arena: std.heap.ArenaAllocator,
/// The options with either default or set values.
options: Spec,
/// The positional arguments that were passed to the process.
positionals: [][:0]const u8,
/// Name of the executable file (or: zeroth argument)
executable_name: ?[:0]const u8,
pub fn deinit(self: Self) void {
self.arena.child_allocator.free(self.positionals);
if (self.executable_name) |n|
self.arena.child_allocator.free(n);
self.arena.deinit();
}
};
}
/// Returns true if the given type requires an argument to be parsed.
fn requiresArg(comptime T: type) bool {
const H = struct {
fn doesArgTypeRequireArg(comptime Type: type) bool {
if (Type == []const u8)
return true;
return switch (@as(std.builtin.TypeId, @typeInfo(Type))) {
.Int, .Float, .Enum => true,
.Bool => false,
.Struct, .Union => true,
else => @compileError(@typeName(Type) ++ " is not a supported argument type!"),
};
}
};
const ti = @typeInfo(T);
if (ti == .Optional) {
return H.doesArgTypeRequireArg(ti.Optional.child);
} else {
return H.doesArgTypeRequireArg(T);
}
}
/// Parses a boolean option.
fn parseBoolean(str: []const u8) !bool {
return if (std.mem.eql(u8, str, "yes"))
true
else if (std.mem.eql(u8, str, "true"))
true
else if (std.mem.eql(u8, str, "y"))
true
else if (std.mem.eql(u8, str, "no"))
false
else if (std.mem.eql(u8, str, "false"))
false
else if (std.mem.eql(u8, str, "n"))
false
else
return error.NotABooleanValue;
}
/// Parses an int option.
fn parseInt(comptime T: type, str: []const u8) !T {
var buf = str;
var multiplier: T = 1;
if (buf.len != 0) {
var base1024 = false;
if (std.ascii.toLower(buf[buf.len - 1]) == 'i') { //ki vs k for instance
buf.len -= 1;
base1024 = true;
}
if (buf.len != 0) {
var pow: u3 = switch (buf[buf.len - 1]) {
'k', 'K' => 1, //kilo
'm', 'M' => 2, //mega
'g', 'G' => 3, //giga
't', 'T' => 4, //tera
'p', 'P' => 5, //peta
else => 0,
};
if (pow != 0) {
buf.len -= 1;
if (comptime std.math.maxInt(T) < 1024)
return error.Overflow;
var base: T = if (base1024) 1024 else 1000;
multiplier = try std.math.powi(T, base, @intCast(T, pow));
}
}
}
const ret: T = switch (@typeInfo(T).Int.signedness) {
.signed => try std.fmt.parseInt(T, buf, 0),
.unsigned => try std.fmt.parseUnsigned(T, buf, 0),
};
return try std.math.mul(T, ret, multiplier);
}
test "parseInt" {
const tst = std.testing;
try tst.expectEqual(@as(i32, 50), try parseInt(i32, "50"));
try tst.expectEqual(@as(i32, 6000), try parseInt(i32, "6k"));
try tst.expectEqual(@as(u32, 2048), try parseInt(u32, "0x2KI"));
try tst.expectEqual(@as(i8, 0), try parseInt(i8, "0"));
try tst.expectEqual(@as(usize, 10_000_000_000), try parseInt(usize, "0xAg"));
try tst.expectError(error.Overflow, parseInt(i2, "1m"));
try tst.expectError(error.Overflow, parseInt(u16, "1Ti"));
}
/// Converts an argument value to the target type.
fn convertArgumentValue(comptime T: type, textInput: []const u8) !T {
if (T == []const u8)
return textInput;
switch (@typeInfo(T)) {
.Optional => |opt| return try convertArgumentValue(opt.child, textInput),
.Bool => if (textInput.len > 0)
return try parseBoolean(textInput)
else
return true, // boolean options are always true
.Int => return try parseInt(T, textInput),
.Float => return try std.fmt.parseFloat(T, textInput),
.Enum => {
if (@hasDecl(T, "parse")) {
return try T.parse(textInput);
} else {
return std.meta.stringToEnum(T, textInput) orelse return error.InvalidEnumeration;
}
},
.Struct, .Union => {
if (@hasDecl(T, "parse")) {
return try T.parse(textInput);
} else {
@compileError(@typeName(T) ++ " has no public visible `fn parse([]const u8) !T`!");
}
},
else => @compileError(@typeName(T) ++ " is not a supported argument type!"),
}
}
/// Parses an option value into the correct type.
fn parseOption(
comptime Spec: type,
result: *ParseArgsResult(Spec),
args: *std.process.ArgIterator,
error_handling: ErrorHandling,
last_error: *?anyerror,
/// The name of the option that is currently parsed.
comptime name: []const u8,
/// Optional pre-defined value for options that use `--foo=bar`
value: ?[]const u8,
) !void {
const field_type = @TypeOf(@field(result.options, name));
const final_value = if (value) |val|
val // use the literal value
else if (requiresArg(field_type))
// fetch from parser
try (args.next(&result.arena.allocator) orelse {
last_error.* = error.MissingArgument;
try error_handling.process(error.MissingArgument, Error{
.option = "--" ++ name,
.kind = .missing_argument,
});
return;
})
else
// argument is "empty"
"";
@field(result.options, name) = convertArgumentValue(field_type, final_value) catch |err| {
last_error.* = err;
try error_handling.process(err, Error{
.option = "--" ++ name,
.kind = .{ .invalid_value = final_value },
});
// we couldn't parse the value, so we return a undefined value as we have signalled an
// error and won't return this anyways.
return undefined;
};
}
/// A collection of errors that were encountered while parsing arguments.
pub const ErrorCollection = struct {
const Self = @This();
arena: std.heap.ArenaAllocator,
list: std.ArrayList(Error),
pub fn init(allocator: *std.mem.Allocator) Self {
return Self{
.arena = std.heap.ArenaAllocator.init(allocator),
.list = std.ArrayList(Error).init(allocator),
};
}
pub fn deinit(self: *Self) void {
self.list.deinit();
self.arena.deinit();
self.* = undefined;
}
/// Returns the current enumeration of errors.
pub fn errors(self: Self) []const Error {
return self.list.items;
}
/// Appends an error to the collection
fn insert(self: *Self, err: Error) !void {
var dupe = Error{
.option = try self.arena.allocator.dupe(u8, err.option),
.kind = switch (err.kind) {
.invalid_value => |v| Error.Kind{
.invalid_value = try self.arena.allocator.dupe(u8, v),
},
// flat copy
.unknown,
.out_of_memory,
.unsupported,
.invalid_placement,
.missing_argument,
.missing_executable_name,
=> err.kind,
},
};
try self.list.append(dupe);
}
};
/// An argument parsing error.
pub const Error = struct {
const Self = @This();
/// The option that yielded the error
option: []const u8,
/// The kind of error, might include additional information
kind: Kind,
pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
_ = options;
switch (self.kind) {
.unknown => try writer.print("The option {s} does not exist", .{self.option}),
.invalid_value => |value| try writer.print("Invalid value '{s}' for option {s}", .{ value, self.option }),
.out_of_memory => try writer.print("Out of memory while parsing option {s}", .{self.option}),
.unsupported => try writer.writeAll("Short command line options are not supported."),
.invalid_placement => try writer.writeAll("An option with argument must be the last option for short command line options."),
.missing_argument => try writer.print("Missing argument for option {s}", .{self.option}),
.missing_executable_name => try writer.writeAll("Failed to get executable name from the argument list!"),
}
}
const Kind = union(enum) {
/// When the argument itself is unknown
unknown,
/// When the parsing of an argument value failed
invalid_value: []const u8,
/// When the parsing of an argument value triggered a out of memory error
out_of_memory,
/// When the argument is a short argument and no shorthands are enabled
unsupported,
/// Can only happen when a shorthand for an option requires an argument, but is followed by more shorthands.
invalid_placement,
/// An option was passed that requires an argument, but the option was passed last.
missing_argument,
/// This error has an empty option name and can only happen when parsing the argument list for a process.
missing_executable_name,
};
};
/// The error handling method that should be used.
pub const ErrorHandling = union(enum) {
const Self = @This();
/// Do not print or process any errors, just
/// return a fitting error on the first argument mismatch.
silent,
/// Print errors to stderr and return a `error.InvalidArguments`.
print,
/// Collect errors into the error collection and return
/// `error.InvalidArguments` when any error was encountered.
collect: *ErrorCollection,
/// Processes an error with the given handling method.
fn process(self: Self, src_error: anytype, err: Error) !void {
if (@typeInfo(@TypeOf(src_error)) != .ErrorSet)
@compileError("src_error must be a error union!");
switch (self) {
.silent => return src_error,
.print => try std.io.getStdErr().writer().print("{}\n", .{err}),
.collect => |collection| try collection.insert(err),
}
}
};
test {
std.testing.refAllDecls(@This());
}
test "ErrorCollection" {
var option_buf = "option".*;
var invalid_buf = "invalid".*;
var ec = ErrorCollection.init(std.testing.allocator);
defer ec.deinit();
try ec.insert(Error{
.option = &option_buf,
.kind = .{ .invalid_value = &invalid_buf },
});
option_buf = undefined;
invalid_buf = undefined;
try std.testing.expectEqualStrings("option", ec.errors()[0].option);
try std.testing.expectEqualStrings("invalid", ec.errors()[0].kind.invalid_value);
}