summaryrefslogtreecommitdiff
path: root/clap/comptime.zig
diff options
context:
space:
mode:
authorGravatar Komari Spaghetti2020-11-02 18:04:30 +0000
committerGravatar Jimmi Holst Christensen2020-11-02 19:05:41 +0100
commit42894f6c8b957541ac0b1830139312047cd8fdc6 (patch)
treef5c6c839334b863f77e665879be0d467614543f2 /clap/comptime.zig
parentuse null sentinel in OsIterator (#27) (diff)
downloadzig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.tar.gz
zig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.tar.xz
zig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.zip
Report error context in Diagnostic (#26)
Diffstat (limited to 'clap/comptime.zig')
-rw-r--r--clap/comptime.zig14
1 files changed, 9 insertions, 5 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig
index b0edb2a..e8aae30 100644
--- a/clap/comptime.zig
+++ b/clap/comptime.zig
@@ -6,7 +6,11 @@ const heap = std.heap;
6const mem = std.mem; 6const mem = std.mem;
7const debug = std.debug; 7const debug = std.debug;
8 8
9pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { 9pub fn ComptimeClap(
10 comptime Id: type,
11 comptime ArgIter: type,
12 comptime params: []const clap.Param(Id),
13) type {
10 var flags: usize = 0; 14 var flags: usize = 0;
11 var single_options: usize = 0; 15 var single_options: usize = 0;
12 var multi_options: usize = 0; 16 var multi_options: usize = 0;
@@ -38,7 +42,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
38 pos: []const []const u8, 42 pos: []const []const u8,
39 allocator: *mem.Allocator, 43 allocator: *mem.Allocator,
40 44
41 pub fn parse(allocator: *mem.Allocator, comptime ArgIter: type, iter: *ArgIter) !@This() { 45 pub fn parse(allocator: *mem.Allocator, iter: *ArgIter, diag: ?*clap.Diagnostic) !@This() {
42 var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; 46 var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options;
43 for (multis) |*multi| { 47 for (multis) |*multi| {
44 multi.* = std.ArrayList([]const u8).init(allocator); 48 multi.* = std.ArrayList([]const u8).init(allocator);
@@ -58,7 +62,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
58 .params = converted_params, 62 .params = converted_params,
59 .iter = iter, 63 .iter = iter,
60 }; 64 };
61 while (try stream.next()) |arg| { 65 while (try stream.next(diag)) |arg| {
62 const param = arg.param; 66 const param = arg.param;
63 if (param.names.long == null and param.names.short == null) { 67 if (param.names.long == null and param.names.short == null) {
64 try pos.append(arg.value.?); 68 try pos.append(arg.value.?);
@@ -143,7 +147,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
143} 147}
144 148
145test "clap.comptime.ComptimeClap" { 149test "clap.comptime.ComptimeClap" {
146 const Clap = ComptimeClap(clap.Help, comptime &[_]clap.Param(clap.Help){ 150 const Clap = ComptimeClap(clap.Help, clap.args.SliceIterator, comptime &[_]clap.Param(clap.Help){
147 clap.parseParam("-a, --aa ") catch unreachable, 151 clap.parseParam("-a, --aa ") catch unreachable,
148 clap.parseParam("-b, --bb ") catch unreachable, 152 clap.parseParam("-b, --bb ") catch unreachable,
149 clap.parseParam("-c, --cc <V>") catch unreachable, 153 clap.parseParam("-c, --cc <V>") catch unreachable,
@@ -160,7 +164,7 @@ test "clap.comptime.ComptimeClap" {
160 "-a", "-c", "0", "something", "-d", "a", "--dd", "b", 164 "-a", "-c", "0", "something", "-d", "a", "--dd", "b",
161 }, 165 },
162 }; 166 };
163 var args = try Clap.parse(&fb_allocator.allocator, clap.args.SliceIterator, &iter); 167 var args = try Clap.parse(&fb_allocator.allocator, &iter, null);
164 defer args.deinit(); 168 defer args.deinit();
165 169
166 testing.expect(args.flag("-a")); 170 testing.expect(args.flag("-a"));