From d02219c58fe2487139da712e2c93d28078b096d1 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Thu, 24 Jun 2021 17:36:35 +0200 Subject: Fix all new compiler errors from zig master --- build.zig | 1 + clap.zig | 2 +- clap/args.zig | 1 + clap/comptime.zig | 8 ++++---- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index 5cbc731..fcb72b5 100644 --- a/build.zig +++ b/build.zig @@ -56,6 +56,7 @@ fn readMeStep(b: *Builder) *std.build.Step { s.* = std.build.Step.init(.Custom, "ReadMeStep", b.allocator, struct { fn make(step: *std.build.Step) anyerror!void { @setEvalBranchQuota(10000); + _ = step; const file = try std.fs.cwd().createFile("README.md", .{}); const stream = file.writer(); try stream.print(@embedFile("example/README.md.template"), .{ diff --git a/clap.zig b/clap.zig index ee2dad8..5bb3542 100644 --- a/clap.zig +++ b/clap.zig @@ -110,7 +110,7 @@ pub fn parseParam(line: []const u8) !Param(Help) { } else null; var res = parseParamRest(it.rest()); - res.names.long = param_str[2..]; + res.names.long = long_name; res.names.short = short_name; return res; } diff --git a/clap/args.zig b/clap/args.zig index d848eb7..1d93189 100644 --- a/clap/args.zig +++ b/clap/args.zig @@ -12,6 +12,7 @@ pub const ExampleArgIterator = struct { const Error = error{}; pub fn next(iter: *ExampleArgIterator) Error!?[]const u8 { + _ = iter; return "2"; } }; diff --git a/clap/comptime.zig b/clap/comptime.zig index cbc872e..0328997 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig @@ -11,10 +11,10 @@ pub fn ComptimeClap( comptime Id: type, comptime params: []const clap.Param(Id), ) type { - var flags: usize = 0; - var single_options: usize = 0; - var multi_options: usize = 0; - var converted_params: []const clap.Param(usize) = &.{}; + comptime var flags: usize = 0; + comptime var single_options: usize = 0; + comptime var multi_options: usize = 0; + comptime var converted_params: []const clap.Param(usize) = &.{}; for (params) |param| { var index: usize = 0; if (param.names.long != null or param.names.short != null) { -- cgit v1.2.3 From f1c0a9fda67a2c720db22bf2c109127ac507a32c Mon Sep 17 00:00:00 2001 From: J.R. "hiljusti" Hill Date: Tue, 6 Jul 2021 02:00:25 -0700 Subject: Update example of `usage` (#45) --- README.md | 6 +++--- example/usage.zig | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4576ace..646c052 100644 --- a/README.md +++ b/README.md @@ -224,9 +224,9 @@ pub fn main() !void { try clap.usage( std.io.getStdErr().writer(), comptime &.{ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-v, --version Output version information and exit.") catch unreachable, - clap.parseParam(" --value Output version information and exit.") catch unreachable, + clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, + clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, + clap.parseParam(" --value An option parameter, which takes a value.") catch unreachable, }, ); } diff --git a/example/usage.zig b/example/usage.zig index 7956570..90fa310 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -8,9 +8,9 @@ pub fn main() !void { try clap.usage( std.io.getStdErr().writer(), comptime &.{ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-v, --version Output version information and exit.") catch unreachable, - clap.parseParam(" --value Output version information and exit.") catch unreachable, + clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, + clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, + clap.parseParam(" --value An option parameter, which takes a value.") catch unreachable, }, ); } -- cgit v1.2.3 From bb8a5ae5b9eec884a50e4977cc5275039ea53207 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Fri, 16 Jul 2021 15:13:57 +0000 Subject: Forward diagnostics down to StreamingClap fixes #46 --- clap/comptime.zig | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/clap/comptime.zig b/clap/comptime.zig index 0328997..a0f57ad 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig @@ -3,6 +3,7 @@ const std = @import("std"); const debug = std.debug; const heap = std.heap; +const io = std.io; const mem = std.mem; const testing = std.testing; @@ -64,6 +65,7 @@ pub fn ComptimeClap( var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){ .params = converted_params, .iter = iter, + .diagnostic = opt.diagnostic, }; while (try stream.next()) |arg| { const param = arg.param; @@ -152,20 +154,20 @@ pub fn ComptimeClap( } test "" { - const Clap = ComptimeClap(clap.Help, comptime &.{ + const params = comptime &.{ clap.parseParam("-a, --aa") catch unreachable, clap.parseParam("-b, --bb") catch unreachable, clap.parseParam("-c, --cc ") catch unreachable, clap.parseParam("-d, --dd ...") catch unreachable, clap.parseParam("

") catch unreachable, - }); + }; var iter = clap.args.SliceIterator{ .args = &.{ "-a", "-c", "0", "something", "-d", "a", "--dd", "b", }, }; - var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); + var args = try clap.parseEx(clap.Help, params, &iter, .{ .allocator = testing.allocator }); defer args.deinit(); try testing.expect(args.flag("-a")); @@ -181,8 +183,55 @@ test "" { } test "empty" { - const Clap = ComptimeClap(clap.Help, comptime &.{}); var iter = clap.args.SliceIterator{ .args = &.{} }; - var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); + var args = try clap.parseEx(u8, &.{}, &iter, .{ .allocator = testing.allocator }); defer args.deinit(); } + +fn testErr( + comptime params: []const clap.Param(u8), + args_strings: []const []const u8, + expected: []const u8, +) !void { + var diag = clap.Diagnostic{}; + var iter = clap.args.SliceIterator{ .args = args_strings }; + var args = clap.parseEx(u8, params, &iter, .{ + .allocator = testing.allocator, + .diagnostic = &diag, + }) catch |err| { + var buf: [1024]u8 = undefined; + var fbs = io.fixedBufferStream(&buf); + diag.report(fbs.writer(), err) catch return error.TestFailed; + try testing.expectEqualStrings(expected, fbs.getWritten()); + return; + }; + + try testing.expect(false); +} + +test "errors" { + const params = [_]clap.Param(u8){ + .{ + .id = 0, + .names = .{ .short = 'a', .long = "aa" }, + }, + .{ + .id = 1, + .names = .{ .short = 'c', .long = "cc" }, + .takes_value = .one, + }, + }; + + try testErr(¶ms, &.{"q"}, "Invalid argument 'q'\n"); + try testErr(¶ms, &.{"-q"}, "Invalid argument '-q'\n"); + try testErr(¶ms, &.{"--q"}, "Invalid argument '--q'\n"); + try testErr(¶ms, &.{"--q=1"}, "Invalid argument '--q'\n"); + try testErr(¶ms, &.{"-a=1"}, "The argument '-a' does not take a value\n"); + try testErr(¶ms, &.{"--aa=1"}, "The argument '--aa' does not take a value\n"); + try testErr(¶ms, &.{"-c"}, "The argument '-c' requires a value but none was supplied\n"); + try testErr( + ¶ms, + &.{"--cc"}, + "The argument '--cc' requires a value but none was supplied\n", + ); +} -- cgit v1.2.3