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