summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Asherah Connor2020-08-23 12:21:26 +1000
committerGravatar Jimmi Holst Christensen2020-09-06 17:31:27 +0200
commit1180b1c2b52ae4f3f9aaf7e8308b3a46ee828eed (patch)
tree87e41829de3a62a93932877cfe46a72ea72b6120
parentFix expected type error on 32 bit systems (diff)
downloadzig-clap-1180b1c2b52ae4f3f9aaf7e8308b3a46ee828eed.tar.gz
zig-clap-1180b1c2b52ae4f3f9aaf7e8308b3a46ee828eed.tar.xz
zig-clap-1180b1c2b52ae4f3f9aaf7e8308b3a46ee828eed.zip
parse multiple options
-rw-r--r--clap.zig4
-rw-r--r--clap/comptime.zig31
2 files changed, 26 insertions, 9 deletions
diff --git a/clap.zig b/clap.zig
index 1711af9..588fa47 100644
--- a/clap.zig
+++ b/clap.zig
@@ -247,6 +247,10 @@ pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type {
247 return a.clap.option(name); 247 return a.clap.option(name);
248 } 248 }
249 249
250 pub fn allOptions(a: @This(), comptime name: []const u8) [][]const u8 {
251 return a.clap.allOptions(name);
252 }
253
250 pub fn positionals(a: @This()) []const []const u8 { 254 pub fn positionals(a: @This()) []const []const u8 {
251 return a.clap.positionals(); 255 return a.clap.positionals();
252 } 256 }
diff --git a/clap/comptime.zig b/clap/comptime.zig
index c25659b..90d34e9 100644
--- a/clap/comptime.zig
+++ b/clap/comptime.zig
@@ -27,7 +27,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
27 } 27 }
28 28
29 return struct { 29 return struct {
30 options: [options]?[]const u8, 30 options: [options]std.ArrayList([]const u8),
31 flags: [flags]bool, 31 flags: [flags]bool,
32 pos: []const []const u8, 32 pos: []const []const u8,
33 allocator: *mem.Allocator, 33 allocator: *mem.Allocator,
@@ -35,11 +35,14 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
35 pub fn parse(allocator: *mem.Allocator, comptime ArgIter: type, iter: *ArgIter) !@This() { 35 pub fn parse(allocator: *mem.Allocator, comptime ArgIter: type, iter: *ArgIter) !@This() {
36 var pos = std.ArrayList([]const u8).init(allocator); 36 var pos = std.ArrayList([]const u8).init(allocator);
37 var res = @This(){ 37 var res = @This(){
38 .options = [_]?[]const u8{null} ** options, 38 .options = [_]std.ArrayList([]const u8){undefined} ** options,
39 .flags = [_]bool{false} ** flags, 39 .flags = [_]bool{false} ** flags,
40 .pos = undefined, 40 .pos = undefined,
41 .allocator = allocator, 41 .allocator = allocator,
42 }; 42 };
43 for (res.options) |*init_opt| {
44 init_opt.* = std.ArrayList([]const u8).init(allocator);
45 }
43 46
44 var stream = clap.StreamingClap(usize, ArgIter){ 47 var stream = clap.StreamingClap(usize, ArgIter){
45 .params = converted_params, 48 .params = converted_params,
@@ -56,7 +59,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
56 59
57 // Hack: Utilize Zigs lazy analyzis to avoid a compiler error 60 // Hack: Utilize Zigs lazy analyzis to avoid a compiler error
58 if (res.options.len != 0) 61 if (res.options.len != 0)
59 res.options[param.id] = arg.value.?; 62 try res.options[param.id].append(arg.value.?);
60 } else { 63 } else {
61 debug.assert(res.flags.len != 0); 64 debug.assert(res.flags.len != 0);
62 if (res.flags.len != 0) 65 if (res.flags.len != 0)
@@ -69,6 +72,8 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
69 } 72 }
70 73
71 pub fn deinit(parser: *@This()) void { 74 pub fn deinit(parser: *@This()) void {
75 for (parser.options) |o|
76 o.deinit();
72 parser.allocator.free(parser.pos); 77 parser.allocator.free(parser.pos);
73 parser.* = undefined; 78 parser.* = undefined;
74 } 79 }
@@ -81,12 +86,17 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id))
81 return parser.flags[param.id]; 86 return parser.flags[param.id];
82 } 87 }
83 88
84 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { 89 pub fn allOptions(parser: @This(), comptime name: []const u8) [][]const u8 {
85 const param = comptime findParam(name); 90 const param = comptime findParam(name);
86 if (!param.takes_value) 91 if (!param.takes_value)
87 @compileError(name ++ " is a flag and not an option."); 92 @compileError(name ++ " is a flag and not an option.");
88 93
89 return parser.options[param.id]; 94 return parser.options[param.id].items;
95 }
96
97 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 {
98 const items = parser.allOptions(name);
99 return if (items.len > 0) items[0] else null;
90 } 100 }
91 101
92 pub fn positionals(parser: @This()) []const []const u8 { 102 pub fn positionals(parser: @This()) []const []const u8 {
@@ -117,6 +127,7 @@ test "clap.comptime.ComptimeClap" {
117 clap.parseParam("-a, --aa ") catch unreachable, 127 clap.parseParam("-a, --aa ") catch unreachable,
118 clap.parseParam("-b, --bb ") catch unreachable, 128 clap.parseParam("-b, --bb ") catch unreachable,
119 clap.parseParam("-c, --cc <V>") catch unreachable, 129 clap.parseParam("-c, --cc <V>") catch unreachable,
130 clap.parseParam("-d, --dd <V>") catch unreachable,
120 clap.Param(clap.Help){ 131 clap.Param(clap.Help){
121 .takes_value = true, 132 .takes_value = true,
122 }, 133 },
@@ -126,7 +137,7 @@ test "clap.comptime.ComptimeClap" {
126 var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]); 137 var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]);
127 var iter = clap.args.SliceIterator{ 138 var iter = clap.args.SliceIterator{
128 .args = &[_][]const u8{ 139 .args = &[_][]const u8{
129 "-a", "-c", "0", "something", 140 "-a", "-c", "0", "something", "-d", "a", "--dd", "b",
130 }, 141 },
131 }; 142 };
132 var args = try Clap.parse(&fb_allocator.allocator, clap.args.SliceIterator, &iter); 143 var args = try Clap.parse(&fb_allocator.allocator, clap.args.SliceIterator, &iter);
@@ -136,8 +147,10 @@ test "clap.comptime.ComptimeClap" {
136 testing.expect(args.flag("--aa")); 147 testing.expect(args.flag("--aa"));
137 testing.expect(!args.flag("-b")); 148 testing.expect(!args.flag("-b"));
138 testing.expect(!args.flag("--bb")); 149 testing.expect(!args.flag("--bb"));
139 testing.expectEqualSlices(u8, "0", args.option("-c").?); 150 testing.expectEqualStrings("0", args.option("-c").?);
140 testing.expectEqualSlices(u8, "0", args.option("--cc").?); 151 testing.expectEqualStrings("0", args.option("--cc").?);
141 testing.expectEqual(@as(usize, 1), args.positionals().len); 152 testing.expectEqual(@as(usize, 1), args.positionals().len);
142 testing.expectEqualSlices(u8, "something", args.positionals()[0]); 153 testing.expectEqualStrings("something", args.positionals()[0]);
154 testing.expectEqualStrings("a", args.option("-d").?);
155 testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.allOptions("--dd"));
143} 156}