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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
const std = @import("std");
const debug = std.debug;
const heap = std.heap;
const io = std.io;
const mem = std.mem;
const testing = std.testing;
pub const args = @import("clap/args.zig");
test "clap" {
testing.refAllDecls(@This());
}
pub const ComptimeClap = @import("clap/comptime.zig").ComptimeClap;
pub const StreamingClap = @import("clap/streaming.zig").StreamingClap;
/// The names a ::Param can have.
pub const Names = struct {
/// '-' prefix
short: ?u8 = null,
/// '--' prefix
long: ?[]const u8 = null,
};
/// Whether a param takes no value (a flag), one value, or can be specified multiple times.
pub const Values = enum {
none,
one,
many,
one_optional,
};
/// Represents a parameter for the command line.
/// Parameters come in three kinds:
/// * Short ("-a"): Should be used for the most commonly used parameters in your program.
/// * They can take a value three different ways.
/// * "-a value"
/// * "-a=value"
/// * "-avalue"
/// * They chain if they don't take values: "-abc".
/// * The last given parameter can take a value in the same way that a single parameter can:
/// * "-abc value"
/// * "-abc=value"
/// * "-abcvalue"
/// * Long ("--long-param"): Should be used for less common parameters, or when no single character
/// can describe the paramter.
/// * They can take a value two different ways.
/// * "--long-param value"
/// * "--long-param=value"
/// * Positional: Should be used as the primary parameter of the program, like a filename or
/// an expression to parse.
/// * Positional parameters have both names.long and names.short == null.
/// * Positional parameters must take a value.
pub fn Param(comptime Id: type) type {
return struct {
id: Id = std.mem.zeroes(Id),
names: Names = std.mem.zeroes(Names),
takes_value: Values = .none,
};
}
/// Takes a string and parses it to a Param(Help).
/// This is the reverse of 'help' but for at single parameter only.
pub fn parseParam(line: []const u8) !Param(Help) {
@setEvalBranchQuota(999999);
var found_comma = false;
var it = mem.tokenize(u8, line, " \t");
var param_str = it.next() orelse return error.NoParamFound;
const short_name = if (!mem.startsWith(u8, param_str, "--") and
mem.startsWith(u8, param_str, "-"))
blk: {
found_comma = param_str[param_str.len - 1] == ',';
if (found_comma)
param_str = param_str[0 .. param_str.len - 1];
if (param_str.len != 2)
return error.InvalidShortParam;
const short_name = param_str[1];
if (!found_comma) {
var res = parseParamRest(it.rest());
res.names.short = short_name;
return res;
}
param_str = it.next() orelse return error.NoParamFound;
break :blk short_name;
} else null;
_ = if (mem.startsWith(u8, param_str, "--")) {
if (param_str[param_str.len - 1] == ',')
return error.TrailingComma;
} else if (found_comma) {
return error.TrailingComma;
} else if (short_name == null) {
return parseParamRest(mem.trimLeft(u8, line, " \t"));
} else null;
var res = parseParamRest(it.rest());
res.names.long = param_str[2..];
res.names.short = short_name;
return res;
}
fn parseParamRest(line: []const u8) Param(Help) {
if (mem.startsWith(u8, line, "<")) blk: {
const len = mem.indexOfScalar(u8, line, '>') orelse break :blk;
const takes_many = mem.startsWith(u8, line[len + 1 ..], "...");
const takes_one_optional = mem.startsWith(u8, line[len + 1 ..], "?");
const help_start = len + 1 + @as(usize, 3) * @intFromBool(takes_many) + (@as(usize, 1) * @intFromBool(takes_one_optional));
return .{
.takes_value = if (takes_many) Values.many else if (takes_one_optional) Values.one_optional else Values.one,
.id = .{
.msg = mem.trim(u8, line[help_start..], " \t"),
.value = line[1..len],
},
};
}
return .{ .id = .{ .msg = mem.trim(u8, line, " \t") } };
}
fn expectParam(expect: Param(Help), actual: Param(Help)) void {
testing.expectEqualStrings(expect.id.msg, actual.id.msg);
testing.expectEqualStrings(expect.id.value, actual.id.value);
testing.expectEqual(expect.names.short, actual.names.short);
testing.expectEqual(expect.takes_value, actual.takes_value);
if (expect.names.long) |long| {
testing.expectEqualStrings(long, actual.names.long.?);
} else {
testing.expectEqual(@as(?[]const u8, null), actual.names.long);
}
}
test "parseParam" {
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "value" },
.names = .{ .short = 's', .long = "long" },
.takes_value = .one,
}, try parseParam("-s, --long <value> Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "value" },
.names = .{ .short = 's', .long = "long" },
.takes_value = .many,
}, try parseParam("-s, --long <value>... Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "value" },
.names = .{ .long = "long" },
.takes_value = .one,
}, try parseParam("--long <value> Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "value" },
.names = .{ .short = 's' },
.takes_value = .one,
}, try parseParam("-s <value> Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text" },
.names = .{ .short = 's', .long = "long" },
}, try parseParam("-s, --long Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text" },
.names = .{ .short = 's' },
}, try parseParam("-s Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text" },
.names = .{ .long = "long" },
}, try parseParam("--long Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "A | B" },
.names = .{ .long = "long" },
.takes_value = .one,
}, try parseParam("--long <A | B> Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "A" },
.names = .{},
.takes_value = .one,
}, try parseParam("<A> Help text"));
expectParam(Param(Help){
.id = .{ .msg = "Help text", .value = "A" },
.names = .{},
.takes_value = .many,
}, try parseParam("<A>... Help text"));
testing.expectError(error.TrailingComma, parseParam("--long, Help"));
testing.expectError(error.TrailingComma, parseParam("-s, Help"));
testing.expectError(error.InvalidShortParam, parseParam("-ss Help"));
testing.expectError(error.InvalidShortParam, parseParam("-ss <value> Help"));
testing.expectError(error.InvalidShortParam, parseParam("- Help"));
}
/// Optional diagnostics used for reporting useful errors
pub const Diagnostic = struct {
arg: []const u8 = "",
name: Names = Names{},
/// Default diagnostics reporter when all you want is English with no colors.
/// Use this as a reference for implementing your own if needed.
pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void {
const Arg = struct {
prefix: []const u8,
name: []const u8,
};
const a = if (diag.name.short) |*c|
Arg{ .prefix = "-", .name = @as(*const [1]u8, c)[0..] }
else if (diag.name.long) |l|
Arg{ .prefix = "--", .name = l }
else
Arg{ .prefix = "", .name = diag.arg };
switch (err) {
error.DoesntTakeValue => try stream.print("The argument '{s}{s}' does not take a value\n", .{ a.prefix, a.name }),
error.MissingValue => try stream.print("The argument '{s}{s}' requires a value but none was supplied\n", .{ a.prefix, a.name }),
error.InvalidArgument => if (a.prefix.len > 0 and a.name.len > 0)
try stream.print("Invalid argument '{s}{s}'\n", .{ a.prefix, a.name })
else
try stream.print("Failed to parse argument due to unexpected single dash\n", .{}),
else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}),
}
}
};
fn testDiag(diag: Diagnostic, err: anyerror, expected: []const u8) void {
var buf: [1024]u8 = undefined;
var slice_stream = io.fixedBufferStream(&buf);
diag.report(slice_stream.writer(), err) catch unreachable;
testing.expectEqualStrings(expected, slice_stream.getWritten());
}
pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type {
return struct {
arena: @import("root").bun.ArenaAllocator,
clap: ComptimeClap(Id, params),
exe_arg: ?[]const u8,
pub fn deinit(a: *@This()) void {
a.arena.deinit();
}
pub fn flag(a: @This(), comptime name: []const u8) bool {
return a.clap.flag(name);
}
pub fn option(a: @This(), comptime name: []const u8) ?[]const u8 {
return a.clap.option(name);
}
pub fn options(a: @This(), comptime name: []const u8) []const []const u8 {
return a.clap.options(name);
}
pub fn positionals(a: @This()) []const []const u8 {
return a.clap.positionals();
}
pub fn remaining(a: @This()) []const []const u8 {
return a.clap.remaining();
}
pub fn hasFlag(comptime name: []const u8) bool {
return ComptimeClap(Id, params).hasFlag(name);
}
};
}
/// Options that can be set to customize the behavior of parsing.
pub const ParseOptions = struct {
/// The allocator used for all memory allocations. Defaults to the `heap.page_allocator`.
/// Note: You should probably override this allocator if you are calling `parseEx`. Unlike
/// `parse`, `parseEx` does not wrap the allocator so the heap allocator can be
/// quite expensive. (TODO: Can we pick a better default? For `parse`, this allocator
/// is fine, as it wraps it in an arena)
allocator: mem.Allocator = heap.page_allocator,
diagnostic: ?*Diagnostic = null,
stop_after_positional_at: usize = 0,
};
/// Same as `parseEx` but uses the `args.OsIterator` by default.
pub fn parse(
comptime Id: type,
comptime params: []const Param(Id),
opt: ParseOptions,
) !Args(Id, params) {
var iter = args.OsIterator.init(opt.allocator);
var res = Args(Id, params){
.arena = iter.arena,
.exe_arg = iter.exe_arg,
.clap = undefined,
};
// Let's reuse the arena from the `OSIterator` since we already have
// it.
res.clap = try parseEx(Id, params, &iter, .{
.allocator = res.arena.allocator(),
.diagnostic = opt.diagnostic,
.stop_after_positional_at = opt.stop_after_positional_at,
});
return res;
}
/// Parses the command line arguments passed into the program based on an
/// array of `Param`s.
pub fn parseEx(
comptime Id: type,
comptime params: []const Param(Id),
iter: anytype,
opt: ParseOptions,
) !ComptimeClap(Id, params) {
const Clap = ComptimeClap(Id, params);
return try Clap.parse(iter, opt);
}
/// Will print a help message in the following format:
/// -s, --long <valueText> helpText
/// -s, helpText
/// -s <valueText> helpText
/// --long helpText
/// --long <valueText> helpText
pub fn helpFull(
stream: anytype,
comptime Id: type,
params: []const Param(Id),
comptime Error: type,
context: anytype,
helpText: fn (@TypeOf(context), Param(Id)) Error![]const u8,
valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8,
) !void {
const max_spacing = blk: {
var res: usize = 0;
for (params) |param| {
var cs = io.countingWriter(io.null_writer);
try printParam(cs.writer(), Id, param, Error, context, valueText);
if (res < cs.bytes_written)
res = @intCast(usize, cs.bytes_written);
}
break :blk res;
};
for (params) |param| {
if (param.names.short == null and param.names.long == null)
continue;
var cs = io.countingWriter(stream);
try stream.print("\t", .{});
try printParam(cs.writer(), Id, param, Error, context, valueText);
try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written));
try stream.print("\t{s}\n", .{try helpText(context, param)});
}
}
fn printParam(
stream: anytype,
comptime Id: type,
param: Param(Id),
comptime Error: type,
context: anytype,
valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8,
) !void {
if (param.names.short) |s| {
try stream.print("-{c}", .{s});
} else {
try stream.print(" ", .{});
}
if (param.names.long) |l| {
if (param.names.short) |_| {
try stream.print(", ", .{});
} else {
try stream.print(" ", .{});
}
try stream.print("--{s}", .{l});
}
switch (param.takes_value) {
.none => {},
.one => try stream.print(" <{s}>", .{try valueText(context, param)}),
.one_optional => try stream.print(" <{s}>?", .{try valueText(context, param)}),
.many => try stream.print(" <{s}>...", .{try valueText(context, param)}),
}
}
/// A wrapper around helpFull for simple helpText and valueText functions that
/// cant return an error or take a context.
pub fn helpEx(
stream: anytype,
comptime Id: type,
params: []const Param(Id),
helpText: *const fn (Param(Id)) []const u8,
valueText: *const fn (Param(Id)) []const u8,
) !void {
const Context = struct {
helpText: *const fn (Param(Id)) []const u8,
valueText: *const fn (Param(Id)) []const u8,
pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 {
return c.helpText(p);
}
pub fn value(c: @This(), p: Param(Id)) error{}![]const u8 {
return c.valueText(p);
}
};
return helpFull(
stream,
Id,
params,
error{},
Context{
.helpText = helpText,
.valueText = valueText,
},
Context.help,
Context.value,
);
}
pub const Help = struct {
msg: []const u8 = "",
value: []const u8 = "",
};
/// A wrapper around helpEx that takes a Param(Help).
pub fn help(stream: anytype, params: []const Param(Help)) !void {
try helpEx(stream, Help, params, getHelpSimple, getValueSimple);
}
fn getHelpSimple(param: Param(Help)) []const u8 {
return param.id.msg;
}
fn getValueSimple(param: Param(Help)) []const u8 {
return param.id.value;
}
/// Will print a usage message in the following format:
/// [-abc] [--longa] [-d <valueText>] [--longb <valueText>] <valueText>
///
/// First all none value taking parameters, which have a short name are
/// printed, then non positional parameters and finally the positinal.
pub fn usageFull(
stream: anytype,
comptime Id: type,
params: []const Param(Id),
comptime Error: type,
context: anytype,
valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8,
) !void {
var cos = io.countingWriter(stream);
const cs = cos.writer();
for (params) |param| {
const name = param.names.short orelse continue;
if (param.takes_value != .none)
continue;
if (cos.bytes_written == 0)
try stream.writeAll("[-");
try cs.writeByte(name);
}
if (cos.bytes_written != 0)
try cs.writeByte(']');
var positional: ?Param(Id) = null;
for (params) |param| {
if (param.takes_value == .none and param.names.short != null)
continue;
const prefix = if (param.names.short) |_| "-" else "--";
// Seems the zig compiler is being a little wierd. I doesn't allow me to write
// @as(*const [1]u8, s) VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
const name = if (param.names.short) |*s| @ptrCast([*]const u8, s)[0..1] else param.names.long orelse {
positional = param;
continue;
};
if (cos.bytes_written != 0)
try cs.writeByte(' ');
try cs.print("[{s}{s}", .{ prefix, name });
switch (param.takes_value) {
.none => {},
.one => try cs.print(" <{s}>", .{try valueText(context, param)}),
.one_optional => try cs.print(" <{s}>?", .{try valueText(context, param)}),
.many => try cs.print(" <{s}>...", .{try valueText(context, param)}),
}
try cs.writeByte(']');
}
if (positional) |p| {
if (cos.bytes_written != 0)
try cs.writeByte(' ');
try cs.print("<{s}>", .{try valueText(context, p)});
}
}
/// A wrapper around usageFull for a simple valueText functions that
/// cant return an error or take a context.
pub fn usageEx(
stream: anytype,
comptime Id: type,
params: []const Param(Id),
valueText: fn (Param(Id)) []const u8,
) !void {
const Context = struct {
valueText: fn (Param(Id)) []const u8,
pub fn value(c: @This(), p: Param(Id)) error{}![]const u8 {
return c.valueText(p);
}
};
return usageFull(
stream,
Id,
params,
error{},
Context{ .valueText = valueText },
Context.value,
);
}
/// A wrapper around usageEx that takes a Param(Help).
pub fn usage(stream: anytype, params: []const Param(Help)) !void {
try usageEx(stream, Help, params, getValueSimple);
}
fn testUsage(expected: []const u8, params: []const Param(Help)) !void {
var buf: [1024]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);
try usage(fbs.writer(), params);
testing.expectEqualStrings(expected, fbs.getWritten());
}
|