From aa334d8c1df252f48960e0253eb25544678a6023 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Mon, 26 Apr 2021 16:23:15 +0200 Subject: Refactor Diagnostic (and others) into a ParseOption struct This allows for default arguments, which we can also extend without breaking peoples code in the future. This is a breaking change right now though. --- example/simple-ex.zig | 14 ++++++++------ example/simple.zig | 11 +++++------ example/streaming-clap.zig | 15 +++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) (limited to 'example') diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d6ecc44..f504d63 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -1,5 +1,5 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); const debug = std.debug; @@ -21,11 +21,13 @@ pub fn main() !void { 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; - - var args = clap.parseEx(clap.Help, ¶ms, allocator, &iter, &diag) catch |err| { + // This is optional. You can also pass `.{}` to `clap.parse` if you don't + // care about the extra information `Diagnostics` provides. + var diag = clap.Diagnostic{}; + var args = clap.parseEx(clap.Help, ¶ms, &iter, .{ + .allocator = allocator, + .diagnostic = &diag, + }) catch |err| { // Report useful error and exit diag.report(std.io.getStdErr().outStream(), err) catch {}; return err; diff --git a/example/simple.zig b/example/simple.zig index 270e344..392dca3 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -1,5 +1,5 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); const debug = std.debug; @@ -14,11 +14,10 @@ pub fn main() !void { }; // 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; - - var args = clap.parse(clap.Help, ¶ms, std.heap.page_allocator, &diag) catch |err| { + // This is optional. You can also pass `.{}` to `clap.parse` if you don't + // care about the extra information `Diagnostics` provides. + var diag = clap.Diagnostic{}; + var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { // Report useful error and exit diag.report(std.io.getStdErr().outStream(), err) catch {}; return err; diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 941070f..f8d873d 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -1,5 +1,5 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); const debug = std.debug; @@ -28,19 +28,18 @@ pub fn main() !void { var iter = try clap.args.OsIterator.init(allocator); defer iter.deinit(); - // Initialize our streaming parser. + // Initalize our diagnostics, which can be used for reporting useful errors. + // This is optional. You can also leave the `diagnostic` field unset if you + // don't care about the extra information `Diagnostic` provides. + var diag = clap.Diagnostic{}; var parser = clap.StreamingClap(u8, clap.args.OsIterator){ .params = ¶ms, .iter = &iter, + .diagnostic = &diag, }; - // 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 (parser.next(&diag) catch |err| { + while (parser.next() catch |err| { // Report useful error and exit diag.report(std.io.getStdErr().outStream(), err) catch {}; return err; -- cgit v1.2.3