summaryrefslogtreecommitdiff
path: root/example/streaming-clap.zig
diff options
context:
space:
mode:
Diffstat (limited to 'example/streaming-clap.zig')
-rw-r--r--example/streaming-clap.zig35
1 files changed, 18 insertions, 17 deletions
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index 5468fd5..32d44c4 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -1,26 +1,24 @@
1const std = @import("std");
2const clap = @import("clap"); 1const clap = @import("clap");
2const std = @import("std");
3 3
4const debug = std.debug; 4const debug = std.debug;
5const io = std.io;
5 6
6pub fn main() !void { 7pub fn main() !void {
7 const allocator = std.heap.page_allocator; 8 const allocator = std.heap.page_allocator;
8 9
9 // First we specify what parameters our program can take. 10 // First we specify what parameters our program can take.
10 const params = [_]clap.Param(u8){ 11 const params = [_]clap.Param(u8){
11 clap.Param(u8){ 12 .{
12 .id = 'h', 13 .id = 'h',
13 .names = clap.Names{ .short = 'h', .long = "help" }, 14 .names = .{ .short = 'h', .long = "help" },
14 }, 15 },
15 clap.Param(u8){ 16 .{
16 .id = 'n', 17 .id = 'n',
17 .names = clap.Names{ .short = 'n', .long = "number" }, 18 .names = .{ .short = 'n', .long = "number" },
18 .takes_value = .One, 19 .takes_value = .one,
19 },
20 clap.Param(u8){
21 .id = 'f',
22 .takes_value = .One,
23 }, 20 },
21 .{ .id = 'f', .takes_value = .one },
24 }; 22 };
25 23
26 // We then initialize an argument iterator. We will use the OsIterator as it nicely 24 // We then initialize an argument iterator. We will use the OsIterator as it nicely
@@ -28,21 +26,24 @@ pub fn main() !void {
28 var iter = try clap.args.OsIterator.init(allocator); 26 var iter = try clap.args.OsIterator.init(allocator);
29 defer iter.deinit(); 27 defer iter.deinit();
30 28
31 // Initialize our streaming parser. 29 // Initalize our diagnostics, which can be used for reporting useful errors.
30 // This is optional. You can also leave the `diagnostic` field unset if you
31 // don't care about the extra information `Diagnostic` provides.
32 var diag = clap.Diagnostic{};
32 var parser = clap.StreamingClap(u8, clap.args.OsIterator){ 33 var parser = clap.StreamingClap(u8, clap.args.OsIterator){
33 .params = &params, 34 .params = &params,
34 .iter = &iter, 35 .iter = &iter,
36 .diagnostic = &diag,
35 }; 37 };
36 38
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
42 // Because we use a streaming parser, we have to consume each argument parsed individually. 39 // Because we use a streaming parser, we have to consume each argument parsed individually.
43 while (parser.next(&diag) catch |err| { 40 while (parser.next() catch |err| {
44 // Report useful error and exit 41 // Report useful error and exit
42<<<<<<< HEAD
45 diag.report(std.io.getStdErr().writer(), err) catch {}; 43 diag.report(std.io.getStdErr().writer(), err) catch {};
44=======
45 diag.report(io.getStdErr().writer(), err) catch {};
46>>>>>>> master
46 return err; 47 return err;
47 }) |arg| { 48 }) |arg| {
48 // arg.param will point to the parameter which matched the argument. 49 // arg.param will point to the parameter which matched the argument.