summaryrefslogtreecommitdiff
path: root/example/comptime-clap.zig
diff options
context:
space:
mode:
authorGravatar Komari Spaghetti2020-11-02 18:04:30 +0000
committerGravatar GitHub2020-11-02 18:04:30 +0000
commit093d29899b8fdf449b944e973b07b5992be2144a (patch)
treefb669157fa6e17523b22635c93f920bd4cc57760 /example/comptime-clap.zig
parentadjust examples, README template (diff)
downloadzig-clap-093d29899b8fdf449b944e973b07b5992be2144a.tar.gz
zig-clap-093d29899b8fdf449b944e973b07b5992be2144a.tar.xz
zig-clap-093d29899b8fdf449b944e973b07b5992be2144a.zip
Report error context in Diagnostic (#26)
Diffstat (limited to '')
-rw-r--r--example/comptime-clap.zig12
1 files changed, 11 insertions, 1 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"))