From 9db6e56d1603a5fe738516b10a3b15b9209e4680 Mon Sep 17 00:00:00 2001 From: Komari Spaghetti Date: Thu, 24 Jun 2021 17:22:33 +0200 Subject: Stay under 100 chars per line in all the code --- clap.zig | 120 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 33 deletions(-) (limited to 'clap.zig') diff --git a/clap.zig b/clap.zig index 1e47b42..ee2dad8 100644 --- a/clap.zig +++ b/clap.zig @@ -43,8 +43,8 @@ pub const Values = enum { /// * "-abc value" /// * "-abc=value" /// * "-abcvalue" -/// * Long ("--long-param"): Should be used for less common parameters, or when no single character -/// can describe the paramter. +/// * Long ("--long-param"): Should be used for less common parameters, or when no single +/// character can describe the paramter. /// * They can take a value two different ways. /// * "--long-param value" /// * "--long-param=value" @@ -229,9 +229,18 @@ pub const Diagnostic = struct { Arg{ .prefix = "", .name = diag.arg }; switch (err) { - error.DoesntTakeValue => try stream.print("The argument '{s}{s}' does not take a value\n", .{ a.prefix, a.name }), - error.MissingValue => try stream.print("The argument '{s}{s}' requires a value but none was supplied\n", .{ a.prefix, a.name }), - error.InvalidArgument => try stream.print("Invalid argument '{s}{s}'\n", .{ a.prefix, a.name }), + error.DoesntTakeValue => try stream.print( + "The argument '{s}{s}' does not take a value\n", + .{ a.prefix, a.name }, + ), + error.MissingValue => try stream.print( + "The argument '{s}{s}' requires a value but none was supplied\n", + .{ a.prefix, a.name }, + ), + error.InvalidArgument => try stream.print( + "Invalid argument '{s}{s}'\n", + .{ a.prefix, a.name }, + ), else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}), } } @@ -246,15 +255,51 @@ fn testDiag(diag: Diagnostic, err: anyerror, expected: []const u8) !void { test "Diagnostic.report" { try testDiag(.{ .arg = "c" }, error.InvalidArgument, "Invalid argument 'c'\n"); - try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n"); - try testDiag(.{ .name = .{ .short = 'c' } }, error.DoesntTakeValue, "The argument '-c' does not take a value\n"); - try testDiag(.{ .name = .{ .long = "cc" } }, error.DoesntTakeValue, "The argument '--cc' does not take a value\n"); - try testDiag(.{ .name = .{ .short = 'c' } }, error.MissingValue, "The argument '-c' requires a value but none was supplied\n"); - try testDiag(.{ .name = .{ .long = "cc" } }, error.MissingValue, "The argument '--cc' requires a value but none was supplied\n"); - try testDiag(.{ .name = .{ .short = 'c' } }, error.InvalidArgument, "Invalid argument '-c'\n"); - try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n"); - try testDiag(.{ .name = .{ .short = 'c' } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n"); - try testDiag(.{ .name = .{ .long = "cc" } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n"); + try testDiag( + .{ .name = .{ .long = "cc" } }, + error.InvalidArgument, + "Invalid argument '--cc'\n", + ); + try testDiag( + .{ .name = .{ .short = 'c' } }, + error.DoesntTakeValue, + "The argument '-c' does not take a value\n", + ); + try testDiag( + .{ .name = .{ .long = "cc" } }, + error.DoesntTakeValue, + "The argument '--cc' does not take a value\n", + ); + try testDiag( + .{ .name = .{ .short = 'c' } }, + error.MissingValue, + "The argument '-c' requires a value but none was supplied\n", + ); + try testDiag( + .{ .name = .{ .long = "cc" } }, + error.MissingValue, + "The argument '--cc' requires a value but none was supplied\n", + ); + try testDiag( + .{ .name = .{ .short = 'c' } }, + error.InvalidArgument, + "Invalid argument '-c'\n", + ); + try testDiag( + .{ .name = .{ .long = "cc" } }, + error.InvalidArgument, + "Invalid argument '--cc'\n", + ); + try testDiag( + .{ .name = .{ .short = 'c' } }, + error.SomethingElse, + "Error while parsing arguments: SomethingElse\n", + ); + try testDiag( + .{ .name = .{ .long = "cc" } }, + error.SomethingElse, + "Error while parsing arguments: SomethingElse\n", + ); } pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { @@ -466,7 +511,9 @@ test "clap.help" { parseParam("-c, --cc Both flag.") catch unreachable, parseParam("-d, --dd Both option.") catch unreachable, parseParam("-d, --dd ... Both repeated option.") catch unreachable, - parseParam("

Positional. This should not appear in the help message.") catch unreachable, + parseParam( + "

Positional. This should not appear in the help message.", + ) catch unreachable, }, ); @@ -516,12 +563,16 @@ pub fn usageFull( const prefix = if (param.names.short) |_| "-" else "--"; - // Seems the zig compiler is being a little wierd. I doesn't allow me to write - // @as(*const [1]u8, s) VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV - const name = if (param.names.short) |*s| @ptrCast([*]const u8, s)[0..1] else param.names.long orelse { - positional = param; - continue; - }; + const name = if (param.names.short) |*s| + // Seems the zig compiler is being a little wierd. I doesn't allow me to write + // @as(*const [1]u8, s) + @ptrCast([*]const u8, s)[0..1] + else + param.names.long orelse { + positional = param; + continue; + }; + if (cos.bytes_written != 0) try cs.writeByte(' '); @@ -601,16 +652,19 @@ test "usage" { try testUsage("", &.{ try parseParam(""), }); - try testUsage("[-ab] [-c ] [-d ] [--e] [--f] [--g ] [--h ] [-i ...] ", &.{ - try parseParam("-a"), - try parseParam("-b"), - try parseParam("-c "), - try parseParam("-d "), - try parseParam("--e"), - try parseParam("--f"), - try parseParam("--g "), - try parseParam("--h "), - try parseParam("-i ..."), - try parseParam(""), - }); + try testUsage( + "[-ab] [-c ] [-d ] [--e] [--f] [--g ] [--h ] [-i ...] ", + &.{ + try parseParam("-a"), + try parseParam("-b"), + try parseParam("-c "), + try parseParam("-d "), + try parseParam("--e"), + try parseParam("--f"), + try parseParam("--g "), + try parseParam("--h "), + try parseParam("-i ..."), + try parseParam(""), + }, + ); } -- cgit v1.2.3