summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--README.md56
-rw-r--r--build.zig3
-rw-r--r--clap.zig21
-rw-r--r--clap/comptime.zig8
-rw-r--r--example/README.md.template11
-rw-r--r--example/simple-ex.zig (renamed from example/comptime-clap.zig)4
6 files changed, 26 insertions, 77 deletions
diff --git a/README.md b/README.md
index ebcebbb..3156539 100644
--- a/README.md
+++ b/README.md
@@ -99,59 +99,7 @@ zig-clap/example/simple-error.zig:16:18: note: called from here
99 _ = args.flag("--helps"); 99 _ = args.flag("--helps");
100``` 100```
101 101
102### `ComptimeClap` 102There is also a `parseEx` variant that takes an argument iterator.
103
104The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument
105iterator.
106
107```zig
108const std = @import("std");
109const clap = @import("clap");
110
111const debug = std.debug;
112
113pub fn main() !void {
114 const allocator = std.heap.page_allocator;
115
116 // First we specify what parameters our program can take.
117 // We can use `parseParam` to parse a string to a `Param(Help)`
118 const params = comptime [_]clap.Param(clap.Help){
119 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
120 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
121 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
122 clap.parseParam("<POS>...") catch unreachable,
123 };
124 const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, &params);
125
126 // We then initialize an argument iterator. We will use the OsIterator as it nicely
127 // wraps iterating over arguments the most efficient way on each os.
128 var iter = try clap.args.OsIterator.init(allocator);
129 defer iter.deinit();
130
131 // Initalize our diagnostics, which can be used for reporting useful errors.
132 // This is optional. You can also just pass `null` to `parser.next` if you
133 // don't care about the extra information `Diagnostics` provides.
134 var diag: clap.Diagnostic = undefined;
135
136 // Parse the arguments
137 var args = Clap.parse(allocator, &iter, &diag) catch |err| {
138 // Report useful error and exit
139 diag.report(std.io.getStdErr().outStream(), err) catch {};
140 return err;
141 };
142 defer args.deinit();
143
144 if (args.flag("--help"))
145 debug.warn("--help\n", .{});
146 if (args.option("--number")) |n|
147 debug.warn("--number = {}\n", .{n});
148 for (args.options("--string")) |s|
149 debug.warn("--string = {}\n", .{s});
150 for (args.positionals()) |pos|
151 debug.warn("{}\n", .{pos});
152}
153
154```
155 103
156### `StreamingClap` 104### `StreamingClap`
157 105
@@ -222,7 +170,7 @@ pub fn main() !void {
222 170
223``` 171```
224 172
225Currently, this parse is the only parser that allow an array of `Param` that 173Currently, this parse is the only parser that allow an array of `Param` tha
226is generated at runtime. 174is generated at runtime.
227 175
228### `help` 176### `help`
diff --git a/build.zig b/build.zig
index 9baf1d4..c32f4c2 100644
--- a/build.zig
+++ b/build.zig
@@ -32,8 +32,8 @@ pub fn build(b: *Builder) void {
32 const example_step = b.step("examples", "Build examples"); 32 const example_step = b.step("examples", "Build examples");
33 inline for ([_][]const u8{ 33 inline for ([_][]const u8{
34 "simple", 34 "simple",
35 "simple-ex",
35 //"simple-error", 36 //"simple-error",
36 "comptime-clap",
37 "streaming-clap", 37 "streaming-clap",
38 "help", 38 "help",
39 "usage", 39 "usage",
@@ -70,7 +70,6 @@ fn readMeStep(b: *Builder) *std.build.Step {
70 try stream.print(@embedFile("example/README.md.template"), .{ 70 try stream.print(@embedFile("example/README.md.template"), .{
71 @embedFile("example/simple.zig"), 71 @embedFile("example/simple.zig"),
72 @embedFile("example/simple-error.zig"), 72 @embedFile("example/simple-error.zig"),
73 @embedFile("example/comptime-clap.zig"),
74 @embedFile("example/streaming-clap.zig"), 73 @embedFile("example/streaming-clap.zig"),
75 @embedFile("example/help.zig"), 74 @embedFile("example/help.zig"),
76 @embedFile("example/usage.zig"), 75 @embedFile("example/usage.zig"),
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,
diff --git a/example/README.md.template b/example/README.md.template
index 65b507d..530cea4 100644
--- a/example/README.md.template
+++ b/example/README.md.template
@@ -50,14 +50,7 @@ zig-clap/example/simple-error.zig:16:18: note: called from here
50 _ = args.flag("--helps"); 50 _ = args.flag("--helps");
51``` 51```
52 52
53### `ComptimeClap` 53There is also a `parseEx` variant that takes an argument iterator.
54
55The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument
56iterator.
57
58```zig
59{}
60```
61 54
62### `StreamingClap` 55### `StreamingClap`
63 56
@@ -68,7 +61,7 @@ The `StreamingClap` is the base of all the other parsers. It's a streaming parse
68{} 61{}
69``` 62```
70 63
71Currently, this parse is the only parser that allow an array of `Param` that 64Currently, this parse is the only parser that allow an array of `Param` tha
72is generated at runtime. 65is generated at runtime.
73 66
74### `help` 67### `help`
diff --git a/example/comptime-clap.zig b/example/simple-ex.zig
index e5d02ff..d6ecc44 100644
--- a/example/comptime-clap.zig
+++ b/example/simple-ex.zig
@@ -14,7 +14,6 @@ pub fn main() !void {
14 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 14 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
15 clap.parseParam("<POS>...") catch unreachable, 15 clap.parseParam("<POS>...") catch unreachable,
16 }; 16 };
17 const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, &params);
18 17
19 // We then initialize an argument iterator. We will use the OsIterator as it nicely 18 // We then initialize an argument iterator. We will use the OsIterator as it nicely
20 // wraps iterating over arguments the most efficient way on each os. 19 // wraps iterating over arguments the most efficient way on each os.
@@ -26,8 +25,7 @@ pub fn main() !void {
26 // don't care about the extra information `Diagnostics` provides. 25 // don't care about the extra information `Diagnostics` provides.
27 var diag: clap.Diagnostic = undefined; 26 var diag: clap.Diagnostic = undefined;
28 27
29 // Parse the arguments 28 var args = clap.parseEx(clap.Help, &params, allocator, &iter, &diag) catch |err| {
30 var args = Clap.parse(allocator, &iter, &diag) catch |err| {
31 // Report useful error and exit 29 // Report useful error and exit
32 diag.report(std.io.getStdErr().outStream(), err) catch {}; 30 diag.report(std.io.getStdErr().outStream(), err) catch {};
33 return err; 31 return err;