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 From 4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Sat, 8 May 2021 18:08:52 +0200 Subject: Modernize codebase * Better naming for variables * Follow naming style of enums * Use `writer()` instead of `outStream()` * Change many initializers to be a one liner * Don't explicitly initialize fields to their default value --- example/help.zig | 7 ++----- example/simple-error.zig | 4 ++-- example/simple-ex.zig | 3 ++- example/simple.zig | 3 ++- example/streaming-clap.zig | 18 ++++++++---------- example/usage.zig | 6 ++---- 6 files changed, 18 insertions(+), 23 deletions(-) (limited to 'example') diff --git a/example/help.zig b/example/help.zig index 2775177..3cf9e42 100644 --- a/example/help.zig +++ b/example/help.zig @@ -1,15 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr_file = std.io.getStdErr(); - var stderr_out_stream = stderr_file.outStream(); - // clap.help is a function that can print a simple help message, given a // slice of Param(Help). There is also a helpEx, which can print a // help message for any Param, but it is more verbose to call. try clap.help( - stderr_out_stream, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, diff --git a/example/simple-error.zig b/example/simple-error.zig index 3c62f0e..c04a9c6 100644 --- a/example/simple-error.zig +++ b/example/simple-error.zig @@ -1,12 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { const params = comptime [_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit.") catch unreachable, }; - var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); + var args = try clap.parse(clap.Help, ¶ms, .{}); defer args.deinit(); _ = args.flag("--helps"); diff --git a/example/simple-ex.zig b/example/simple-ex.zig index f504d63..f08751b 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -2,6 +2,7 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { const allocator = std.heap.page_allocator; @@ -29,7 +30,7 @@ pub fn main() !void { .diagnostic = &diag, }) catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }; defer args.deinit(); diff --git a/example/simple.zig b/example/simple.zig index 392dca3..69473fa 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -2,6 +2,7 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { // First we specify what parameters our program can take. @@ -19,7 +20,7 @@ pub fn main() !void { 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 {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }; defer args.deinit(); diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index f8d873d..41efd1f 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -2,25 +2,23 @@ const clap = @import("clap"); const std = @import("std"); const debug = std.debug; +const io = std.io; pub fn main() !void { const allocator = std.heap.page_allocator; // First we specify what parameters our program can take. const params = [_]clap.Param(u8){ - clap.Param(u8){ + .{ .id = 'h', - .names = clap.Names{ .short = 'h', .long = "help" }, + .names = .{ .short = 'h', .long = "help" }, }, - clap.Param(u8){ + .{ .id = 'n', - .names = clap.Names{ .short = 'n', .long = "number" }, - .takes_value = .One, - }, - clap.Param(u8){ - .id = 'f', - .takes_value = .One, + .names = .{ .short = 'n', .long = "number" }, + .takes_value = .one, }, + .{ .id = 'f', .takes_value = .one }, }; // We then initialize an argument iterator. We will use the OsIterator as it nicely @@ -41,7 +39,7 @@ pub fn main() !void { // Because we use a streaming parser, we have to consume each argument parsed individually. while (parser.next() catch |err| { // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; + diag.report(io.getStdErr().writer(), err) catch {}; return err; }) |arg| { // arg.param will point to the parameter which matched the argument. diff --git a/example/usage.zig b/example/usage.zig index 25e1a34..e044f1d 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -1,14 +1,12 @@ -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); pub fn main() !void { - const stderr = std.io.getStdErr().outStream(); - // clap.usage is a function that can print a simple usage message, given a // slice of Param(Help). There is also a usageEx, which can print a // usage message for any Param, but it is more verbose to call. try clap.usage( - stderr, + std.io.getStdErr().writer(), comptime &[_]clap.Param(clap.Help){ clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, clap.parseParam("-v, --version Output version information and exit.") catch unreachable, -- cgit v1.2.3