summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
Diffstat (limited to 'example')
-rw-r--r--example/comptime-clap.zig12
-rw-r--r--example/simple-error.zig4
-rw-r--r--example/simple.zig11
-rw-r--r--example/streaming-clap.zig11
4 files changed, 32 insertions, 6 deletions
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig
index d709e48..530c7e6 100644
--- a/example/comptime-clap.zig
+++ b/example/comptime-clap.zig
@@ -16,14 +16,24 @@ pub fn main() !void {
16 .takes_value = .One, 16 .takes_value = .One,
17 }, 17 },
18 }; 18 };
19 const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, &params);
19 20
20 // We then initialize an argument iterator. We will use the OsIterator as it nicely 21 // We then initialize an argument iterator. We will use the OsIterator as it nicely
21 // wraps iterating over arguments the most efficient way on each os. 22 // wraps iterating over arguments the most efficient way on each os.
22 var iter = try clap.args.OsIterator.init(allocator); 23 var iter = try clap.args.OsIterator.init(allocator);
23 defer iter.deinit(); 24 defer iter.deinit();
24 25
26 // Initalize our diagnostics, which can be used for reporting useful errors.
27 // This is optional. You can also just pass `null` to `parser.next` if you
28 // don't care about the extra information `Diagnostics` provides.
29 var diag: clap.Diagnostic = undefined;
30
25 // Parse the arguments 31 // Parse the arguments
26 var args = try clap.ComptimeClap(clap.Help, &params).parse(allocator, clap.args.OsIterator, &iter); 32 var args = Clap.parse(allocator, &iter, &diag) catch |err| {
33 // Report useful error and exit
34 diag.report(std.io.getStdErr().outStream(), err) catch {};
35 return err;
36 };
27 defer args.deinit(); 37 defer args.deinit();
28 38
29 if (args.flag("--help")) 39 if (args.flag("--help"))
diff --git a/example/simple-error.zig b/example/simple-error.zig
index 2c403fc..3c62f0e 100644
--- a/example/simple-error.zig
+++ b/example/simple-error.zig
@@ -2,13 +2,11 @@ const std = @import("std");
2const clap = @import("clap"); 2const clap = @import("clap");
3 3
4pub fn main() !void { 4pub fn main() !void {
5 // First we specify what parameters our program can take.
6 // We can use `parseParam` to parse a string to a `Param(Help)`
7 const params = comptime [_]clap.Param(clap.Help){ 5 const params = comptime [_]clap.Param(clap.Help){
8 clap.parseParam("-h, --help Display this help and exit.") catch unreachable, 6 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
9 }; 7 };
10 8
11 var args = try clap.parse(clap.Help, &params, std.heap.direct_allocator); 9 var args = try clap.parse(clap.Help, &params, std.heap.direct_allocator, null);
12 defer args.deinit(); 10 defer args.deinit();
13 11
14 _ = args.flag("--helps"); 12 _ = args.flag("--helps");
diff --git a/example/simple.zig b/example/simple.zig
index adea9f9..f7b5953 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -15,7 +15,16 @@ pub fn main() !void {
15 }, 15 },
16 }; 16 };
17 17
18 var args = try clap.parse(clap.Help, &params, std.heap.page_allocator); 18 // Initalize our diagnostics, which can be used for reporting useful errors.
19 // This is optional. You can also just pass `null` to `parser.next` if you
20 // don't care about the extra information `Diagnostics` provides.
21 var diag: clap.Diagnostic = undefined;
22
23 var args = clap.parse(clap.Help, &params, std.heap.page_allocator, &diag) catch |err| {
24 // Report useful error and exit
25 diag.report(std.io.getStdErr().outStream(), err) catch {};
26 return err;
27 };
19 defer args.deinit(); 28 defer args.deinit();
20 29
21 if (args.flag("--help")) 30 if (args.flag("--help"))
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index b92a9e6..941070f 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -34,8 +34,17 @@ pub fn main() !void {
34 .iter = &iter, 34 .iter = &iter,
35 }; 35 };
36 36
37 // Initalize our diagnostics, which can be used for reporting useful errors.
38 // This is optional. You can also just pass `null` to `parser.next` if you
39 // don't care about the extra information `Diagnostics` provides.
40 var diag: clap.Diagnostic = undefined;
41
37 // Because we use a streaming parser, we have to consume each argument parsed individually. 42 // Because we use a streaming parser, we have to consume each argument parsed individually.
38 while (try parser.next()) |arg| { 43 while (parser.next(&diag) catch |err| {
44 // Report useful error and exit
45 diag.report(std.io.getStdErr().outStream(), err) catch {};
46 return err;
47 }) |arg| {
39 // arg.param will point to the parameter which matched the argument. 48 // arg.param will point to the parameter which matched the argument.
40 switch (arg.param.id) { 49 switch (arg.param.id) {
41 'h' => debug.warn("Help!\n", .{}), 50 'h' => debug.warn("Help!\n", .{}),