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
|
const builtin = @import("builtin");
const clap = @import("../clap.zig");
const std = @import("std");
const args = clap.args;
const debug = std.debug;
const heap = std.heap;
const io = std.io;
const mem = std.mem;
const os = std.os;
const testing = std.testing;
/// The result returned from StreamingClap.next
pub fn Arg(comptime Id: type) type {
return struct {
const Self = @This();
param: *const clap.Param(Id),
value: ?[]const u8 = null,
};
}
/// A command line argument parser which, given an ArgIterator, will parse arguments according
/// to the params. StreamingClap parses in an iterating manner, so you have to use a loop together with
/// StreamingClap.next to parse all the arguments of your program.
pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
return struct {
const State = union(enum) {
normal,
chaining: Chaining,
rest_are_positional,
const Chaining = struct {
arg: []const u8,
index: usize,
};
};
params: []const clap.Param(Id),
iter: *ArgIterator,
state: State = .normal,
positional: ?*const clap.Param(Id) = null,
diagnostic: ?*clap.Diagnostic = null,
/// Get the next Arg that matches a Param.
pub fn next(parser: *@This()) !?Arg(Id) {
switch (parser.state) {
.normal => return try parser.normal(),
.chaining => |state| return try parser.chainging(state),
.rest_are_positional => {
const param = parser.positionalParam() orelse unreachable;
const value = parser.iter.next() orelse return null;
return Arg(Id){ .param = param, .value = value };
},
}
}
fn normal(parser: *@This()) !?Arg(Id) {
const ArgType = Arg(Id);
const arg_info = (try parser.parseNextArg()) orelse return null;
const arg = arg_info.arg;
switch (arg_info.kind) {
.long => {
const eql_index = mem.indexOfScalar(u8, arg, '=');
const name = if (eql_index) |i| arg[0..i] else arg;
const maybe_value = if (eql_index) |i| arg[i + 1 ..] else null;
for (parser.params) |*param| {
const match = param.names.long orelse continue;
if (!mem.eql(u8, name, match))
continue;
if (param.takes_value == .none) {
if (maybe_value != null)
return parser.err(arg, .{ .long = name }, error.DoesntTakeValue);
return ArgType{ .param = param };
}
const value = blk: {
if (maybe_value) |v|
break :blk v;
break :blk parser.iter.next() orelse brk2: {
if (param.takes_value != .one_optional)
return parser.err(arg, .{ .long = name }, error.MissingValue);
break :brk2 "";
};
};
return ArgType{ .param = param, .value = value };
}
return null;
},
.short => return try parser.chainging(.{
.arg = arg,
.index = 0,
}),
.positional => if (parser.positionalParam()) |param| {
// If we find a positional with the value `--` then we
// interpret the rest of the arguments as positional
// arguments.
if (mem.eql(u8, arg, "--")) {
parser.state = .rest_are_positional;
const value = parser.iter.next() orelse return null;
return Arg(Id){ .param = param, .value = value };
}
return Arg(Id){ .param = param, .value = arg };
} else {
return parser.err(arg, .{}, error.InvalidArgument);
},
}
}
fn chainging(parser: *@This(), state: State.Chaining) !?Arg(Id) {
const arg = state.arg;
const index = state.index;
const next_index = index + 1;
for (parser.params) |*param| {
const short = param.names.short orelse continue;
if (short != arg[index])
continue;
// Before we return, we have to set the new state of the clap
defer {
if (arg.len <= next_index or param.takes_value != .none) {
parser.state = .normal;
} else {
parser.state = .{
.chaining = .{
.arg = arg,
.index = next_index,
},
};
}
}
const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false;
if (param.takes_value == .none or param.takes_value == .one_optional) {
if (next_is_eql and param.takes_value == .none)
return parser.err(arg, .{ .short = short }, error.DoesntTakeValue);
return Arg(Id){ .param = param };
}
if (arg.len <= next_index) {
const value = parser.iter.next() orelse
return parser.err(arg, .{ .short = short }, error.MissingValue);
return Arg(Id){ .param = param, .value = value };
}
if (next_is_eql)
return Arg(Id){ .param = param, .value = arg[next_index + 1 ..] };
return Arg(Id){ .param = param, .value = arg[next_index..] };
}
return parser.err(arg, .{ .short = arg[index] }, error.InvalidArgument);
}
fn positionalParam(parser: *@This()) ?*const clap.Param(Id) {
if (parser.positional) |p|
return p;
for (parser.params) |*param| {
if (param.names.long) |_|
continue;
if (param.names.short) |_|
continue;
parser.positional = param;
return param;
}
return null;
}
const ArgInfo = struct {
arg: []const u8,
kind: enum {
long,
short,
positional,
},
};
fn parseNextArg(parser: *@This()) !?ArgInfo {
const full_arg = parser.iter.next() orelse return null;
if (mem.eql(u8, full_arg, "--") or mem.eql(u8, full_arg, "-"))
return ArgInfo{ .arg = full_arg, .kind = .positional };
if (mem.startsWith(u8, full_arg, "--"))
return ArgInfo{ .arg = full_arg[2..], .kind = .long };
if (mem.startsWith(u8, full_arg, "-"))
return ArgInfo{ .arg = full_arg[1..], .kind = .short };
return ArgInfo{ .arg = full_arg, .kind = .positional };
}
fn err(parser: @This(), arg: []const u8, names: clap.Names, _err: anytype) @TypeOf(_err) {
if (parser.diagnostic) |d|
d.* = .{ .arg = arg, .name = names };
return _err;
}
};
}
fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) void {
var iter = args.SliceIterator{ .args = args_strings };
var c = StreamingClap(u8, args.SliceIterator){
.params = params,
.iter = &iter,
};
for (results) |res| {
const arg = (c.next() catch unreachable) orelse unreachable;
testing.expectEqual(res.param, arg.param);
const expected_value = res.value orelse {
testing.expectEqual(@as(@TypeOf(arg.value), null), arg.value);
continue;
};
const actual_value = arg.value orelse unreachable;
testing.expectEqualSlices(u8, expected_value, actual_value);
}
if (c.next() catch unreachable) |_|
unreachable;
}
fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) void {
var diag = clap.Diagnostic{};
var iter = args.SliceIterator{ .args = args_strings };
var c = StreamingClap(u8, args.SliceIterator){
.params = params,
.iter = &iter,
.diagnostic = &diag,
};
while (c.next() catch |err| {
var buf: [1024]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);
diag.report(fbs.writer(), err) catch unreachable;
testing.expectEqualStrings(expected, fbs.getWritten());
return;
}) |_| {}
testing.expect(false);
}
test "short params" {
const params = [_]clap.Param(u8){
.{ .id = 0, .names = .{ .short = 'a' } },
.{ .id = 1, .names = .{ .short = 'b' } },
.{
.id = 2,
.names = .{ .short = 'c' },
.takes_value = .one,
},
.{
.id = 3,
.names = .{ .short = 'd' },
.takes_value = .many,
},
};
const a = ¶ms[0];
const b = ¶ms[1];
const c = ¶ms[2];
const d = ¶ms[3];
testNoErr(
¶ms,
&[_][]const u8{
"-a", "-b", "-ab", "-ba",
"-c", "0", "-c=0", "-ac",
"0", "-ac=0", "-d=0",
},
&[_]Arg(u8){
.{ .param = a },
.{ .param = b },
.{ .param = a },
.{ .param = b },
.{ .param = b },
.{ .param = a },
.{ .param = c, .value = "0" },
.{ .param = c, .value = "0" },
.{ .param = a },
.{ .param = c, .value = "0" },
.{ .param = a },
.{ .param = c, .value = "0" },
.{ .param = d, .value = "0" },
},
);
}
test "long params" {
const params = [_]clap.Param(u8){
.{ .id = 0, .names = .{ .long = "aa" } },
.{ .id = 1, .names = .{ .long = "bb" } },
.{
.id = 2,
.names = .{ .long = "cc" },
.takes_value = .one,
},
.{
.id = 3,
.names = .{ .long = "dd" },
.takes_value = .many,
},
};
const aa = ¶ms[0];
const bb = ¶ms[1];
const cc = ¶ms[2];
const dd = ¶ms[3];
testNoErr(
¶ms,
&[_][]const u8{
"--aa", "--bb",
"--cc", "0",
"--cc=0", "--dd=0",
},
&[_]Arg(u8){
.{ .param = aa },
.{ .param = bb },
.{ .param = cc, .value = "0" },
.{ .param = cc, .value = "0" },
.{ .param = dd, .value = "0" },
},
);
}
test "positional params" {
const params = [_]clap.Param(u8){.{
.id = 0,
.takes_value = .one,
}};
testNoErr(
¶ms,
&[_][]const u8{ "aa", "bb" },
&[_]Arg(u8){
.{ .param = ¶ms[0], .value = "aa" },
.{ .param = ¶ms[0], .value = "bb" },
},
);
}
test "all params" {
const params = [_]clap.Param(u8){
.{
.id = 0,
.names = .{ .short = 'a', .long = "aa" },
},
.{
.id = 1,
.names = .{ .short = 'b', .long = "bb" },
},
.{
.id = 2,
.names = .{ .short = 'c', .long = "cc" },
.takes_value = .one,
},
.{ .id = 3, .takes_value = .one },
};
const aa = ¶ms[0];
const bb = ¶ms[1];
const cc = ¶ms[2];
const positional = ¶ms[3];
testNoErr(
¶ms,
&[_][]const u8{
"-a", "-b", "-ab", "-ba",
"-c", "0", "-c=0", "-ac",
"0", "-ac=0", "--aa", "--bb",
"--cc", "0", "--cc=0", "something",
"-", "--", "--cc=0", "-a",
},
&[_]Arg(u8){
.{ .param = aa },
.{ .param = bb },
.{ .param = aa },
.{ .param = bb },
.{ .param = bb },
.{ .param = aa },
.{ .param = cc, .value = "0" },
.{ .param = cc, .value = "0" },
.{ .param = aa },
.{ .param = cc, .value = "0" },
.{ .param = aa },
.{ .param = cc, .value = "0" },
.{ .param = aa },
.{ .param = bb },
.{ .param = cc, .value = "0" },
.{ .param = cc, .value = "0" },
.{ .param = positional, .value = "something" },
.{ .param = positional, .value = "-" },
.{ .param = positional, .value = "--cc=0" },
.{ .param = positional, .value = "-a" },
},
);
}
test "errors" {
const params = [_]clap.Param(u8){
.{
.id = 0,
.names = .{ .short = 'a', .long = "aa" },
},
.{
.id = 1,
.names = .{ .short = 'c', .long = "cc" },
.takes_value = .one,
},
};
testErr(¶ms, &[_][]const u8{"q"}, "Invalid argument 'q'\n");
testErr(¶ms, &[_][]const u8{"-q"}, "Invalid argument '-q'\n");
testErr(¶ms, &[_][]const u8{"--q"}, "Invalid argument '--q'\n");
testErr(¶ms, &[_][]const u8{"--q=1"}, "Invalid argument '--q'\n");
testErr(¶ms, &[_][]const u8{"-a=1"}, "The argument '-a' does not take a value\n");
testErr(¶ms, &[_][]const u8{"--aa=1"}, "The argument '--aa' does not take a value\n");
testErr(¶ms, &[_][]const u8{"-c"}, "The argument '-c' requires a value but none was supplied\n");
testErr(¶ms, &[_][]const u8{"--cc"}, "The argument '--cc' requires a value but none was supplied\n");
}
|