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. --- README.md | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 3156539..ad9f026 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ into master on every `zig` release. The simplest way to use this library is to just call the `clap.parse` function. ```zig -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); const debug = std.debug; @@ -41,11 +41,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; @@ -107,8 +106,8 @@ The `StreamingClap` is the base of all the other parsers. It's a streaming parse `args.Iterator` to provide it with arguments lazily. ```zig -const std = @import("std"); const clap = @import("clap"); +const std = @import("std"); const debug = std.debug; @@ -137,19 +136,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 --- README.md | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index ad9f026..398a088 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,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. @@ -46,7 +47,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(); @@ -68,15 +69,15 @@ that the strings you pass to `option`, `options` and `flag` are actually paramet program can take: ```zig -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"); @@ -110,25 +111,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 @@ -149,7 +148,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. @@ -177,18 +176,15 @@ The `help`, `helpEx` and `helpFull` are functions for printing a simple list of program can take. ```zig -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, @@ -218,17 +214,15 @@ The `usage`, `usageEx` and `usageFull` are functions for printing a small abbrev of the help message. ```zig -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