From 42894f6c8b957541ac0b1830139312047cd8fdc6 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Mon, 2 Nov 2020 18:04:30 +0000 Subject: Report error context in Diagnostic (#26) --- example/comptime-clap.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'example/comptime-clap.zig') 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 { .takes_value = .One, }, }; + const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); // We then initialize an argument iterator. We will use the OsIterator as it nicely // wraps iterating over arguments the most efficient way on each os. var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); + // Initalize our diagnostics, which can be used for reporting useful errors. + // This is optional. You can also just pass `null` to `parser.next` if you + // don't care about the extra information `Diagnostics` provides. + var diag: clap.Diagnostic = undefined; + // Parse the arguments - var args = try clap.ComptimeClap(clap.Help, ¶ms).parse(allocator, clap.args.OsIterator, &iter); + var args = Clap.parse(allocator, &iter, &diag) catch |err| { + // Report useful error and exit + diag.report(std.io.getStdErr().outStream(), err) catch {}; + return err; + }; defer args.deinit(); if (args.flag("--help")) -- cgit v1.2.3