From c7d83fcce1739271e399260b50c5f68aa03c5908 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Wed, 26 May 2021 21:06:10 +0200 Subject: Update to latest zig in preperation for 0.8.0 --- clap/args.zig | 86 +++++++++++++++++++++++++++--------------------------- clap/comptime.zig | 70 ++++++++++++++++++++++++++------------------ clap/streaming.zig | 66 ++++++++++++++++++++--------------------- 3 files changed, 118 insertions(+), 104 deletions(-) (limited to 'clap') diff --git a/clap/args.zig b/clap/args.zig index f9ad218..555e8ab 100644 --- a/clap/args.zig +++ b/clap/args.zig @@ -34,8 +34,8 @@ pub const SliceIterator = struct { }; test "SliceIterator" { - const args = &[_][]const u8{ "A", "BB", "CCC" }; - var iter = SliceIterator{ .args = args }; + const args = [_][]const u8{ "A", "BB", "CCC" }; + var iter = SliceIterator{ .args = &args }; for (args) |a| { const b = try iter.next(); @@ -266,76 +266,76 @@ pub const ShellIterator = struct { } }; -fn testShellIteratorOk(str: []const u8, allocations: usize, expect: []const []const u8) void { +fn testShellIteratorOk(str: []const u8, allocations: usize, expect: []const []const u8) !void { var allocator = testing.FailingAllocator.init(testing.allocator, allocations); var it = ShellIterator.init(&allocator.allocator, str); defer it.deinit(); for (expect) |e| { if (it.next()) |actual| { - testing.expect(actual != null); - testing.expectEqualStrings(e, actual.?); - } else |err| testing.expectEqual(@as(anyerror![]const u8, e), err); + try testing.expect(actual != null); + try testing.expectEqualStrings(e, actual.?); + } else |err| try testing.expectEqual(@as(anyerror![]const u8, e), err); } if (it.next()) |actual| { - testing.expectEqual(@as(?[]const u8, null), actual); - testing.expectEqual(allocations, allocator.allocations); - } else |err| testing.expectEqual(@as(anyerror!void, {}), err); + try testing.expectEqual(@as(?[]const u8, null), actual); + try testing.expectEqual(allocations, allocator.allocations); + } else |err| try testing.expectEqual(@as(anyerror!void, {}), err); } -fn testShellIteratorErr(str: []const u8, expect: anyerror) void { +fn testShellIteratorErr(str: []const u8, expect: anyerror) !void { var it = ShellIterator.init(testing.allocator, str); defer it.deinit(); while (it.next() catch |err| { - testing.expectError(expect, @as(anyerror!void, err)); + try testing.expectError(expect, @as(anyerror!void, err)); return; }) |_| {} - testing.expectError(expect, @as(anyerror!void, {})); + try testing.expectError(expect, @as(anyerror!void, {})); } test "ShellIterator" { - testShellIteratorOk("a", 0, &[_][]const u8{"a"}); - testShellIteratorOk("'a'", 0, &[_][]const u8{"a"}); - testShellIteratorOk("\"a\"", 0, &[_][]const u8{"a"}); - testShellIteratorOk("a b", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("'a' b", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("\"a\" b", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("a 'b'", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("a \"b\"", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("'a b'", 0, &[_][]const u8{"a b"}); - testShellIteratorOk("\"a b\"", 0, &[_][]const u8{"a b"}); - testShellIteratorOk("\"a\"\"b\"", 1, &[_][]const u8{"ab"}); - testShellIteratorOk("'a''b'", 1, &[_][]const u8{"ab"}); - testShellIteratorOk("'a'b", 1, &[_][]const u8{"ab"}); - testShellIteratorOk("a'b'", 1, &[_][]const u8{"ab"}); - testShellIteratorOk("a\\ b", 1, &[_][]const u8{"a b"}); - testShellIteratorOk("\"a\\ b\"", 1, &[_][]const u8{"a b"}); - testShellIteratorOk("'a\\ b'", 0, &[_][]const u8{"a\\ b"}); - testShellIteratorOk(" a b ", 0, &[_][]const u8{ "a", "b" }); - testShellIteratorOk("\\ \\ ", 0, &[_][]const u8{ " ", " " }); - - testShellIteratorOk( + try testShellIteratorOk("a", 0, &.{"a"}); + try testShellIteratorOk("'a'", 0, &.{"a"}); + try testShellIteratorOk("\"a\"", 0, &.{"a"}); + try testShellIteratorOk("a b", 0, &.{ "a", "b" }); + try testShellIteratorOk("'a' b", 0, &.{ "a", "b" }); + try testShellIteratorOk("\"a\" b", 0, &.{ "a", "b" }); + try testShellIteratorOk("a 'b'", 0, &.{ "a", "b" }); + try testShellIteratorOk("a \"b\"", 0, &.{ "a", "b" }); + try testShellIteratorOk("'a b'", 0, &.{"a b"}); + try testShellIteratorOk("\"a b\"", 0, &.{"a b"}); + try testShellIteratorOk("\"a\"\"b\"", 1, &.{"ab"}); + try testShellIteratorOk("'a''b'", 1, &.{"ab"}); + try testShellIteratorOk("'a'b", 1, &.{"ab"}); + try testShellIteratorOk("a'b'", 1, &.{"ab"}); + try testShellIteratorOk("a\\ b", 1, &.{"a b"}); + try testShellIteratorOk("\"a\\ b\"", 1, &.{"a b"}); + try testShellIteratorOk("'a\\ b'", 0, &.{"a\\ b"}); + try testShellIteratorOk(" a b ", 0, &.{ "a", "b" }); + try testShellIteratorOk("\\ \\ ", 0, &.{ " ", " " }); + + try testShellIteratorOk( \\printf 'run\nuninstall\n' - , 0, &[_][]const u8{ "printf", "run\\nuninstall\\n" }); - testShellIteratorOk( + , 0, &.{ "printf", "run\\nuninstall\\n" }); + try testShellIteratorOk( \\setsid -f steam "steam://$action/$id" - , 0, &[_][]const u8{ "setsid", "-f", "steam", "steam://$action/$id" }); - testShellIteratorOk( + , 0, &.{ "setsid", "-f", "steam", "steam://$action/$id" }); + try testShellIteratorOk( \\xargs -I% rg --no-heading --no-line-number --only-matching \\ --case-sensitive --multiline --text --byte-offset '(?-u)%' $@ \\ - , 0, &[_][]const u8{ + , 0, &.{ "xargs", "-I%", "rg", "--no-heading", "--no-line-number", "--only-matching", "--case-sensitive", "--multiline", "--text", "--byte-offset", "(?-u)%", "$@", }); - testShellIteratorErr("'a", error.QuoteNotClosed); - testShellIteratorErr("'a\\", error.QuoteNotClosed); - testShellIteratorErr("\"a", error.QuoteNotClosed); - testShellIteratorErr("\"a\\", error.QuoteNotClosed); - testShellIteratorErr("a\\", error.DanglingEscape); + try testShellIteratorErr("'a", error.QuoteNotClosed); + try testShellIteratorErr("'a\\", error.QuoteNotClosed); + try testShellIteratorErr("\"a", error.QuoteNotClosed); + try testShellIteratorErr("\"a\\", error.QuoteNotClosed); + try testShellIteratorErr("a\\", error.DanglingEscape); } diff --git a/clap/comptime.zig b/clap/comptime.zig index 122ff16..7808f52 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig @@ -14,7 +14,7 @@ pub fn ComptimeClap( var flags: usize = 0; var single_options: usize = 0; var multi_options: usize = 0; - var converted_params: []const clap.Param(usize) = &[_]clap.Param(usize){}; + var converted_params: []const clap.Param(usize) = &.{}; for (params) |param| { var index: usize = 0; if (param.names.long != null or param.names.short != null) { @@ -27,18 +27,18 @@ pub fn ComptimeClap( ptr.* += 1; } - const converted = clap.Param(usize){ + converted_params = converted_params ++ [_]clap.Param(usize){.{ .id = index, .names = param.names, .takes_value = param.takes_value, - }; - converted_params = converted_params ++ [_]clap.Param(usize){converted}; + }}; } return struct { - single_options: [single_options]?[]const u8, multi_options: [multi_options][]const []const u8, - flags: [flags]bool, + single_options: [single_options][]const u8, + single_options_is_set: std.PackedIntArray(u1, single_options), + flags: std.PackedIntArray(u1, flags), pos: []const []const u8, allocator: *mem.Allocator, @@ -52,9 +52,12 @@ pub fn ComptimeClap( var pos = std.ArrayList([]const u8).init(allocator); var res = @This(){ - .single_options = [_]?[]const u8{null} ** single_options, - .multi_options = [_][]const []const u8{undefined} ** multi_options, - .flags = [_]bool{false} ** flags, + .multi_options = .{undefined} ** multi_options, + .single_options = .{undefined} ** single_options, + .single_options_is_set = std.PackedIntArray(u1, single_options).init( + .{0} ** single_options, + ), + .flags = std.PackedIntArray(u1, flags).init(.{0} ** flags), .pos = undefined, .allocator = allocator, }; @@ -69,16 +72,18 @@ pub fn ComptimeClap( try pos.append(arg.value.?); } else if (param.takes_value == .one) { debug.assert(res.single_options.len != 0); - if (res.single_options.len != 0) + if (res.single_options.len != 0) { res.single_options[param.id] = arg.value.?; + res.single_options_is_set.set(param.id, 1); + } } else if (param.takes_value == .many) { debug.assert(multis.len != 0); if (multis.len != 0) try multis[param.id].append(arg.value.?); } else { - debug.assert(res.flags.len != 0); - if (res.flags.len != 0) - res.flags[param.id] = true; + debug.assert(res.flags.len() != 0); + if (res.flags.len() != 0) + res.flags.set(param.id, 1); } } @@ -100,7 +105,7 @@ pub fn ComptimeClap( if (param.takes_value != .none) @compileError(name ++ " is an option and not a flag."); - return parser.flags[param.id]; + return parser.flags.get(param.id) != 0; } pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { @@ -109,6 +114,8 @@ pub fn ComptimeClap( @compileError(name ++ " is a flag and not an option."); if (param.takes_value == .many) @compileError(name ++ " takes many options, not one."); + if (parser.single_options_is_set.get(param.id) == 0) + return null; return parser.single_options[param.id]; } @@ -146,30 +153,37 @@ pub fn ComptimeClap( } test "" { - const Clap = ComptimeClap(clap.Help, comptime &[_]clap.Param(clap.Help){ - clap.parseParam("-a, --aa ") catch unreachable, - clap.parseParam("-b, --bb ") catch unreachable, + const Clap = ComptimeClap(clap.Help, 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 = &[_][]const u8{ + .args = &.{ "-a", "-c", "0", "something", "-d", "a", "--dd", "b", }, }; var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); defer args.deinit(); - testing.expect(args.flag("-a")); - testing.expect(args.flag("--aa")); - testing.expect(!args.flag("-b")); - testing.expect(!args.flag("--bb")); - testing.expectEqualStrings("0", args.option("-c").?); - testing.expectEqualStrings("0", args.option("--cc").?); - testing.expectEqual(@as(usize, 1), args.positionals().len); - testing.expectEqualStrings("something", args.positionals()[0]); - testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.options("-d")); - testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.options("--dd")); + try testing.expect(args.flag("-a")); + try testing.expect(args.flag("--aa")); + try testing.expect(!args.flag("-b")); + try testing.expect(!args.flag("--bb")); + try testing.expectEqualStrings("0", args.option("-c").?); + try testing.expectEqualStrings("0", args.option("--cc").?); + try testing.expectEqual(@as(usize, 1), args.positionals().len); + try testing.expectEqualStrings("something", args.positionals()[0]); + try testing.expectEqualSlices([]const u8, &.{ "a", "b" }, args.options("-d")); + try testing.expectEqualSlices([]const u8, &.{ "a", "b" }, args.options("--dd")); +} + +test "empty" { + const Clap = ComptimeClap(clap.Help, comptime &.{}); + var iter = clap.args.SliceIterator{ .args = &.{} }; + var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); + defer args.deinit(); } diff --git a/clap/streaming.zig b/clap/streaming.zig index a2a0ca8..e2f1311 100644 --- a/clap/streaming.zig +++ b/clap/streaming.zig @@ -203,7 +203,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { }; } -fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) void { +fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) !void { var iter = args.SliceIterator{ .args = args_strings }; var c = StreamingClap(u8, args.SliceIterator){ .params = params, @@ -211,22 +211,22 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r }; for (results) |res| { - const arg = (c.next() catch unreachable) orelse unreachable; - testing.expectEqual(res.param, arg.param); + const arg = (try c.next()) orelse return error.TestFailed; + try testing.expectEqual(res.param, arg.param); const expected_value = res.value orelse { - testing.expectEqual(@as(@TypeOf(arg.value), null), arg.value); + try testing.expectEqual(@as(@TypeOf(arg.value), null), arg.value); continue; }; - const actual_value = arg.value orelse unreachable; - testing.expectEqualSlices(u8, expected_value, actual_value); + const actual_value = arg.value orelse return error.TestFailed; + try testing.expectEqualSlices(u8, expected_value, actual_value); } - if (c.next() catch unreachable) |_| - unreachable; + if (try c.next()) |_| + return error.TestFailed; } -fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) void { - var diag = clap.Diagnostic{}; +fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) !void { + var diag: clap.Diagnostic = undefined; var iter = args.SliceIterator{ .args = args_strings }; var c = StreamingClap(u8, args.SliceIterator){ .params = params, @@ -236,12 +236,12 @@ fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, exp while (c.next() catch |err| { var buf: [1024]u8 = undefined; var fbs = io.fixedBufferStream(&buf); - diag.report(fbs.writer(), err) catch unreachable; - testing.expectEqualStrings(expected, fbs.getWritten()); + diag.report(fbs.writer(), err) catch return error.TestFailed; + try testing.expectEqualStrings(expected, fbs.getWritten()); return; }) |_| {} - testing.expect(false); + try testing.expect(false); } test "short params" { @@ -265,14 +265,14 @@ test "short params" { const c = ¶ms[2]; const d = ¶ms[3]; - testNoErr( + try testNoErr( ¶ms, - &[_][]const u8{ + &.{ "-a", "-b", "-ab", "-ba", "-c", "0", "-c=0", "-ac", "0", "-ac=0", "-d=0", }, - &[_]Arg(u8){ + &.{ .{ .param = a }, .{ .param = b }, .{ .param = a }, @@ -311,14 +311,14 @@ test "long params" { const cc = ¶ms[2]; const dd = ¶ms[3]; - testNoErr( + try testNoErr( ¶ms, - &[_][]const u8{ + &.{ "--aa", "--bb", "--cc", "0", "--cc=0", "--dd=0", }, - &[_]Arg(u8){ + &.{ .{ .param = aa }, .{ .param = bb }, .{ .param = cc, .value = "0" }, @@ -334,10 +334,10 @@ test "positional params" { .takes_value = .one, }}; - testNoErr( + try testNoErr( ¶ms, - &[_][]const u8{ "aa", "bb" }, - &[_]Arg(u8){ + &.{ "aa", "bb" }, + &.{ .{ .param = ¶ms[0], .value = "aa" }, .{ .param = ¶ms[0], .value = "bb" }, }, @@ -367,16 +367,16 @@ test "all params" { const cc = ¶ms[2]; const positional = ¶ms[3]; - testNoErr( + try testNoErr( ¶ms, - &[_][]const u8{ + &.{ "-a", "-b", "-ab", "-ba", "-c", "0", "-c=0", "-ac", "0", "-ac=0", "--aa", "--bb", "--cc", "0", "--cc=0", "something", "-", "--", "--cc=0", "-a", }, - &[_]Arg(u8){ + &.{ .{ .param = aa }, .{ .param = bb }, .{ .param = aa }, @@ -413,12 +413,12 @@ test "errors" { .takes_value = .one, }, }; - testErr(¶ms, &[_][]const u8{"q"}, "Invalid argument 'q'\n"); - testErr(¶ms, &[_][]const u8{"-q"}, "Invalid argument '-q'\n"); - testErr(¶ms, &[_][]const u8{"--q"}, "Invalid argument '--q'\n"); - testErr(¶ms, &[_][]const u8{"--q=1"}, "Invalid argument '--q'\n"); - testErr(¶ms, &[_][]const u8{"-a=1"}, "The argument '-a' does not take a value\n"); - testErr(¶ms, &[_][]const u8{"--aa=1"}, "The argument '--aa' does not take a value\n"); - testErr(¶ms, &[_][]const u8{"-c"}, "The argument '-c' requires a value but none was supplied\n"); - testErr(¶ms, &[_][]const u8{"--cc"}, "The argument '--cc' requires a value but none was supplied\n"); + 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