summaryrefslogtreecommitdiff
path: root/clap
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--clap.zig21
-rw-r--r--clap/comptime.zig8
2 files changed, 20 insertions, 9 deletions
diff --git a/clap.zig b/clap.zig
index 8624c88..4548a48 100644
--- a/clap.zig
+++ b/clap.zig
@@ -303,7 +303,7 @@ test "Diagnostic.report" {
303pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { 303pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type {
304 return struct { 304 return struct {
305 arena: std.heap.ArenaAllocator, 305 arena: std.heap.ArenaAllocator,
306 clap: ComptimeClap(Id, args.OsIterator, params), 306 clap: ComptimeClap(Id, params),
307 exe_arg: ?[]const u8, 307 exe_arg: ?[]const u8,
308 308
309 pub fn deinit(a: *@This()) void { 309 pub fn deinit(a: *@This()) void {
@@ -329,8 +329,7 @@ pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type {
329 }; 329 };
330} 330}
331 331
332/// Parses the command line arguments passed into the program based on an 332/// Same as `parseEx` but uses the `args.OsIterator` by default.
333/// array of `Param`s.
334pub fn parse( 333pub fn parse(
335 comptime Id: type, 334 comptime Id: type,
336 comptime params: []const Param(Id), 335 comptime params: []const Param(Id),
@@ -338,8 +337,7 @@ pub fn parse(
338 diag: ?*Diagnostic, 337 diag: ?*Diagnostic,
339) !Args(Id, params) { 338) !Args(Id, params) {
340 var iter = try args.OsIterator.init(allocator); 339 var iter = try args.OsIterator.init(allocator);
341 const Clap = ComptimeClap(Id, args.OsIterator, params); 340 const clap = try parseEx(Id, params, allocator, &iter, diag);
342 const clap = try Clap.parse(allocator, &iter, diag);
343 return Args(Id, params){ 341 return Args(Id, params){
344 .arena = iter.arena, 342 .arena = iter.arena,
345 .clap = clap, 343 .clap = clap,
@@ -347,6 +345,19 @@ pub fn parse(
347 }; 345 };
348} 346}
349 347
348/// Parses the command line arguments passed into the program based on an
349/// array of `Param`s.
350pub fn parseEx(
351 comptime Id: type,
352 comptime params: []const Param(Id),
353 allocator: *mem.Allocator,
354 iter: anytype,
355 diag: ?*Diagnostic,
356) !ComptimeClap(Id, params) {
357 const Clap = ComptimeClap(Id, params);
358 return try Clap.parse(allocator, iter, diag);
359}
360
350/// Will print a help message in the following format: 361/// Will print a help message in the following format:
351/// -s, --long <valueText> helpText 362/// -s, --long <valueText> helpText
352/// -s, helpText 363/// -s, helpText
diff --git a/clap/comptime.zig b/clap/comptime.zig
index 80eb428..8ab61cb 100644
--- a/clap/comptime.zig
+++ b/clap/comptime.zig
@@ -6,9 +6,9 @@ const heap = std.heap;
6const mem = std.mem; 6const mem = std.mem;
7const debug = std.debug; 7const debug = std.debug;
8 8
9/// Deprecated: Use `parseEx` instead
9pub fn ComptimeClap( 10pub fn ComptimeClap(
10 comptime Id: type, 11 comptime Id: type,
11 comptime ArgIter: type,
12 comptime params: []const clap.Param(Id), 12 comptime params: []const clap.Param(Id),
13) type { 13) type {
14 var flags: usize = 0; 14 var flags: usize = 0;
@@ -42,7 +42,7 @@ pub fn ComptimeClap(
42 pos: []const []const u8, 42 pos: []const []const u8,
43 allocator: *mem.Allocator, 43 allocator: *mem.Allocator,
44 44
45 pub fn parse(allocator: *mem.Allocator, iter: *ArgIter, diag: ?*clap.Diagnostic) !@This() { 45 pub fn parse(allocator: *mem.Allocator, iter: anytype, diag: ?*clap.Diagnostic) !@This() {
46 var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; 46 var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options;
47 for (multis) |*multi| { 47 for (multis) |*multi| {
48 multi.* = std.ArrayList([]const u8).init(allocator); 48 multi.* = std.ArrayList([]const u8).init(allocator);
@@ -58,7 +58,7 @@ pub fn ComptimeClap(
58 .allocator = allocator, 58 .allocator = allocator,
59 }; 59 };
60 60
61 var stream = clap.StreamingClap(usize, ArgIter){ 61 var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){
62 .params = converted_params, 62 .params = converted_params,
63 .iter = iter, 63 .iter = iter,
64 }; 64 };
@@ -147,7 +147,7 @@ pub fn ComptimeClap(
147} 147}
148 148
149test "" { 149test "" {
150 const Clap = ComptimeClap(clap.Help, clap.args.SliceIterator, comptime &[_]clap.Param(clap.Help){ 150 const Clap = ComptimeClap(clap.Help, comptime &[_]clap.Param(clap.Help){
151 clap.parseParam("-a, --aa ") catch unreachable, 151 clap.parseParam("-a, --aa ") catch unreachable,
152 clap.parseParam("-b, --bb ") catch unreachable, 152 clap.parseParam("-b, --bb ") catch unreachable,
153 clap.parseParam("-c, --cc <V>") catch unreachable, 153 clap.parseParam("-c, --cc <V>") catch unreachable,