From 093d29899b8fdf449b944e973b07b5992be2144a 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/streaming-clap.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'example/streaming-clap.zig') 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 { .iter = &iter, }; + // 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; + // Because we use a streaming parser, we have to consume each argument parsed individually. - while (try parser.next()) |arg| { + while (parser.next(&diag) catch |err| { + // Report useful error and exit + diag.report(std.io.getStdErr().outStream(), err) catch {}; + return err; + }) |arg| { // arg.param will point to the parameter which matched the argument. switch (arg.param.id) { 'h' => debug.warn("Help!\n", .{}), -- cgit v1.2.3