summaryrefslogtreecommitdiff
path: root/clap
diff options
context:
space:
mode:
Diffstat (limited to 'clap')
-rw-r--r--clap/comptime.zig20
-rw-r--r--clap/streaming.zig184
2 files changed, 87 insertions, 117 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig
index 9bec38e..122ff16 100644
--- a/clap/comptime.zig
+++ b/clap/comptime.zig
@@ -19,9 +19,9 @@ pub fn ComptimeClap(
19 var index: usize = 0; 19 var index: usize = 0;
20 if (param.names.long != null or param.names.short != null) { 20 if (param.names.long != null or param.names.short != null) {
21 const ptr = switch (param.takes_value) { 21 const ptr = switch (param.takes_value) {
22 .None => &flags, 22 .none => &flags,
23 .One => &single_options, 23 .one => &single_options,
24 .Many => &multi_options, 24 .many => &multi_options,
25 }; 25 };
26 index = ptr.*; 26 index = ptr.*;
27 ptr.* += 1; 27 ptr.* += 1;
@@ -67,11 +67,11 @@ pub fn ComptimeClap(
67 const param = arg.param; 67 const param = arg.param;
68 if (param.names.long == null and param.names.short == null) { 68 if (param.names.long == null and param.names.short == null) {
69 try pos.append(arg.value.?); 69 try pos.append(arg.value.?);
70 } else if (param.takes_value == .One) { 70 } else if (param.takes_value == .one) {
71 debug.assert(res.single_options.len != 0); 71 debug.assert(res.single_options.len != 0);
72 if (res.single_options.len != 0) 72 if (res.single_options.len != 0)
73 res.single_options[param.id] = arg.value.?; 73 res.single_options[param.id] = arg.value.?;
74 } else if (param.takes_value == .Many) { 74 } else if (param.takes_value == .many) {
75 debug.assert(multis.len != 0); 75 debug.assert(multis.len != 0);
76 if (multis.len != 0) 76 if (multis.len != 0)
77 try multis[param.id].append(arg.value.?); 77 try multis[param.id].append(arg.value.?);
@@ -97,7 +97,7 @@ pub fn ComptimeClap(
97 97
98 pub fn flag(parser: @This(), comptime name: []const u8) bool { 98 pub fn flag(parser: @This(), comptime name: []const u8) bool {
99 const param = comptime findParam(name); 99 const param = comptime findParam(name);
100 if (param.takes_value != .None) 100 if (param.takes_value != .none)
101 @compileError(name ++ " is an option and not a flag."); 101 @compileError(name ++ " is an option and not a flag.");
102 102
103 return parser.flags[param.id]; 103 return parser.flags[param.id];
@@ -105,18 +105,18 @@ pub fn ComptimeClap(
105 105
106 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { 106 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 {
107 const param = comptime findParam(name); 107 const param = comptime findParam(name);
108 if (param.takes_value == .None) 108 if (param.takes_value == .none)
109 @compileError(name ++ " is a flag and not an option."); 109 @compileError(name ++ " is a flag and not an option.");
110 if (param.takes_value == .Many) 110 if (param.takes_value == .many)
111 @compileError(name ++ " takes many options, not one."); 111 @compileError(name ++ " takes many options, not one.");
112 return parser.single_options[param.id]; 112 return parser.single_options[param.id];
113 } 113 }
114 114
115 pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 { 115 pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 {
116 const param = comptime findParam(name); 116 const param = comptime findParam(name);
117 if (param.takes_value == .None) 117 if (param.takes_value == .none)
118 @compileError(name ++ " is a flag and not an option."); 118 @compileError(name ++ " is a flag and not an option.");
119 if (param.takes_value == .One) 119 if (param.takes_value == .one)
120 @compileError(name ++ " takes one option, not multiple."); 120 @compileError(name ++ " takes one option, not multiple.");
121 121
122 return parser.multi_options[param.id]; 122 return parser.multi_options[param.id];
diff --git a/clap/streaming.zig b/clap/streaming.zig
index 8030a67..a2a0ca8 100644
--- a/clap/streaming.zig
+++ b/clap/streaming.zig
@@ -69,7 +69,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
69 69
70 if (!mem.eql(u8, name, match)) 70 if (!mem.eql(u8, name, match))
71 continue; 71 continue;
72 if (param.takes_value == .None) { 72 if (param.takes_value == .none) {
73 if (maybe_value != null) 73 if (maybe_value != null)
74 return parser.err(arg, .{ .long = name }, error.DoesntTakeValue); 74 return parser.err(arg, .{ .long = name }, error.DoesntTakeValue);
75 75
@@ -122,7 +122,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
122 122
123 // Before we return, we have to set the new state of the clap 123 // Before we return, we have to set the new state of the clap
124 defer { 124 defer {
125 if (arg.len <= next_index or param.takes_value != .None) { 125 if (arg.len <= next_index or param.takes_value != .none) {
126 parser.state = .normal; 126 parser.state = .normal;
127 } else { 127 } else {
128 parser.state = .{ 128 parser.state = .{
@@ -135,7 +135,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type {
135 } 135 }
136 136
137 const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false; 137 const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false;
138 if (param.takes_value == .None) { 138 if (param.takes_value == .none) {
139 if (next_is_eql) 139 if (next_is_eql)
140 return parser.err(arg, .{ .short = short }, error.DoesntTakeValue); 140 return parser.err(arg, .{ .short = short }, error.DoesntTakeValue);
141 return Arg(Id){ .param = param }; 141 return Arg(Id){ .param = param };
@@ -235,9 +235,9 @@ fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, exp
235 }; 235 };
236 while (c.next() catch |err| { 236 while (c.next() catch |err| {
237 var buf: [1024]u8 = undefined; 237 var buf: [1024]u8 = undefined;
238 var slice_stream = io.fixedBufferStream(&buf); 238 var fbs = io.fixedBufferStream(&buf);
239 diag.report(slice_stream.outStream(), err) catch unreachable; 239 diag.report(fbs.writer(), err) catch unreachable;
240 testing.expectEqualStrings(expected, slice_stream.getWritten()); 240 testing.expectEqualStrings(expected, fbs.getWritten());
241 return; 241 return;
242 }) |_| {} 242 }) |_| {}
243 243
@@ -246,23 +246,17 @@ fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, exp
246 246
247test "short params" { 247test "short params" {
248 const params = [_]clap.Param(u8){ 248 const params = [_]clap.Param(u8){
249 clap.Param(u8){ 249 .{ .id = 0, .names = .{ .short = 'a' } },
250 .id = 0, 250 .{ .id = 1, .names = .{ .short = 'b' } },
251 .names = clap.Names{ .short = 'a' }, 251 .{
252 },
253 clap.Param(u8){
254 .id = 1,
255 .names = clap.Names{ .short = 'b' },
256 },
257 clap.Param(u8){
258 .id = 2, 252 .id = 2,
259 .names = clap.Names{ .short = 'c' }, 253 .names = .{ .short = 'c' },
260 .takes_value = .One, 254 .takes_value = .one,
261 }, 255 },
262 clap.Param(u8){ 256 .{
263 .id = 3, 257 .id = 3,
264 .names = clap.Names{ .short = 'd' }, 258 .names = .{ .short = 'd' },
265 .takes_value = .Many, 259 .takes_value = .many,
266 }, 260 },
267 }; 261 };
268 262
@@ -279,42 +273,36 @@ test "short params" {
279 "0", "-ac=0", "-d=0", 273 "0", "-ac=0", "-d=0",
280 }, 274 },
281 &[_]Arg(u8){ 275 &[_]Arg(u8){
282 Arg(u8){ .param = a }, 276 .{ .param = a },
283 Arg(u8){ .param = b }, 277 .{ .param = b },
284 Arg(u8){ .param = a }, 278 .{ .param = a },
285 Arg(u8){ .param = b }, 279 .{ .param = b },
286 Arg(u8){ .param = b }, 280 .{ .param = b },
287 Arg(u8){ .param = a }, 281 .{ .param = a },
288 Arg(u8){ .param = c, .value = "0" }, 282 .{ .param = c, .value = "0" },
289 Arg(u8){ .param = c, .value = "0" }, 283 .{ .param = c, .value = "0" },
290 Arg(u8){ .param = a }, 284 .{ .param = a },
291 Arg(u8){ .param = c, .value = "0" }, 285 .{ .param = c, .value = "0" },
292 Arg(u8){ .param = a }, 286 .{ .param = a },
293 Arg(u8){ .param = c, .value = "0" }, 287 .{ .param = c, .value = "0" },
294 Arg(u8){ .param = d, .value = "0" }, 288 .{ .param = d, .value = "0" },
295 }, 289 },
296 ); 290 );
297} 291}
298 292
299test "long params" { 293test "long params" {
300 const params = [_]clap.Param(u8){ 294 const params = [_]clap.Param(u8){
301 clap.Param(u8){ 295 .{ .id = 0, .names = .{ .long = "aa" } },
302 .id = 0, 296 .{ .id = 1, .names = .{ .long = "bb" } },
303 .names = clap.Names{ .long = "aa" }, 297 .{
304 },
305 clap.Param(u8){
306 .id = 1,
307 .names = clap.Names{ .long = "bb" },
308 },
309 clap.Param(u8){
310 .id = 2, 298 .id = 2,
311 .names = clap.Names{ .long = "cc" }, 299 .names = .{ .long = "cc" },
312 .takes_value = .One, 300 .takes_value = .one,
313 }, 301 },
314 clap.Param(u8){ 302 .{
315 .id = 3, 303 .id = 3,
316 .names = clap.Names{ .long = "dd" }, 304 .names = .{ .long = "dd" },
317 .takes_value = .Many, 305 .takes_value = .many,
318 }, 306 },
319 }; 307 };
320 308
@@ -331,59 +319,47 @@ test "long params" {
331 "--cc=0", "--dd=0", 319 "--cc=0", "--dd=0",
332 }, 320 },
333 &[_]Arg(u8){ 321 &[_]Arg(u8){
334 Arg(u8){ .param = aa }, 322 .{ .param = aa },
335 Arg(u8){ .param = bb }, 323 .{ .param = bb },
336 Arg(u8){ .param = cc, .value = "0" }, 324 .{ .param = cc, .value = "0" },
337 Arg(u8){ .param = cc, .value = "0" }, 325 .{ .param = cc, .value = "0" },
338 Arg(u8){ .param = dd, .value = "0" }, 326 .{ .param = dd, .value = "0" },
339 }, 327 },
340 ); 328 );
341} 329}
342 330
343test "positional params" { 331test "positional params" {
344 const params = [_]clap.Param(u8){clap.Param(u8){ 332 const params = [_]clap.Param(u8){.{
345 .id = 0, 333 .id = 0,
346 .takes_value = .One, 334 .takes_value = .one,
347 }}; 335 }};
348 336
349 testNoErr( 337 testNoErr(
350 &params, 338 &params,
351 &[_][]const u8{ "aa", "bb" }, 339 &[_][]const u8{ "aa", "bb" },
352 &[_]Arg(u8){ 340 &[_]Arg(u8){
353 Arg(u8){ .param = &params[0], .value = "aa" }, 341 .{ .param = &params[0], .value = "aa" },
354 Arg(u8){ .param = &params[0], .value = "bb" }, 342 .{ .param = &params[0], .value = "bb" },
355 }, 343 },
356 ); 344 );
357} 345}
358 346
359test "all params" { 347test "all params" {
360 const params = [_]clap.Param(u8){ 348 const params = [_]clap.Param(u8){
361 clap.Param(u8){ 349 .{
362 .id = 0, 350 .id = 0,
363 .names = clap.Names{ 351 .names = .{ .short = 'a', .long = "aa" },
364 .short = 'a',
365 .long = "aa",
366 },
367 }, 352 },
368 clap.Param(u8){ 353 .{
369 .id = 1, 354 .id = 1,
370 .names = clap.Names{ 355 .names = .{ .short = 'b', .long = "bb" },
371 .short = 'b',
372 .long = "bb",
373 },
374 }, 356 },
375 clap.Param(u8){ 357 .{
376 .id = 2, 358 .id = 2,
377 .names = clap.Names{ 359 .names = .{ .short = 'c', .long = "cc" },
378 .short = 'c', 360 .takes_value = .one,
379 .long = "cc",
380 },
381 .takes_value = .One,
382 },
383 clap.Param(u8){
384 .id = 3,
385 .takes_value = .One,
386 }, 361 },
362 .{ .id = 3, .takes_value = .one },
387 }; 363 };
388 364
389 const aa = &params[0]; 365 const aa = &params[0];
@@ -401,46 +377,40 @@ test "all params" {
401 "-", "--", "--cc=0", "-a", 377 "-", "--", "--cc=0", "-a",
402 }, 378 },
403 &[_]Arg(u8){ 379 &[_]Arg(u8){
404 Arg(u8){ .param = aa }, 380 .{ .param = aa },
405 Arg(u8){ .param = bb }, 381 .{ .param = bb },
406 Arg(u8){ .param = aa }, 382 .{ .param = aa },
407 Arg(u8){ .param = bb }, 383 .{ .param = bb },
408 Arg(u8){ .param = bb }, 384 .{ .param = bb },
409 Arg(u8){ .param = aa }, 385 .{ .param = aa },
410 Arg(u8){ .param = cc, .value = "0" }, 386 .{ .param = cc, .value = "0" },
411 Arg(u8){ .param = cc, .value = "0" }, 387 .{ .param = cc, .value = "0" },
412 Arg(u8){ .param = aa }, 388 .{ .param = aa },
413 Arg(u8){ .param = cc, .value = "0" }, 389 .{ .param = cc, .value = "0" },
414 Arg(u8){ .param = aa }, 390 .{ .param = aa },
415 Arg(u8){ .param = cc, .value = "0" }, 391 .{ .param = cc, .value = "0" },
416 Arg(u8){ .param = aa }, 392 .{ .param = aa },
417 Arg(u8){ .param = bb }, 393 .{ .param = bb },
418 Arg(u8){ .param = cc, .value = "0" }, 394 .{ .param = cc, .value = "0" },
419 Arg(u8){ .param = cc, .value = "0" }, 395 .{ .param = cc, .value = "0" },
420 Arg(u8){ .param = positional, .value = "something" }, 396 .{ .param = positional, .value = "something" },
421 Arg(u8){ .param = positional, .value = "-" }, 397 .{ .param = positional, .value = "-" },
422 Arg(u8){ .param = positional, .value = "--cc=0" }, 398 .{ .param = positional, .value = "--cc=0" },
423 Arg(u8){ .param = positional, .value = "-a" }, 399 .{ .param = positional, .value = "-a" },
424 }, 400 },
425 ); 401 );
426} 402}
427 403
428test "errors" { 404test "errors" {
429 const params = [_]clap.Param(u8){ 405 const params = [_]clap.Param(u8){
430 clap.Param(u8){ 406 .{
431 .id = 0, 407 .id = 0,
432 .names = clap.Names{ 408 .names = .{ .short = 'a', .long = "aa" },
433 .short = 'a',
434 .long = "aa",
435 },
436 }, 409 },
437 clap.Param(u8){ 410 .{
438 .id = 1, 411 .id = 1,
439 .names = clap.Names{ 412 .names = .{ .short = 'c', .long = "cc" },
440 .short = 'c', 413 .takes_value = .one,
441 .long = "cc",
442 },
443 .takes_value = .One,
444 }, 414 },
445 }; 415 };
446 testErr(&params, &[_][]const u8{"q"}, "Invalid argument 'q'\n"); 416 testErr(&params, &[_][]const u8{"q"}, "Invalid argument 'q'\n");