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(""), + }, + ); } diff --git a/clap/args.zig b/clap/args.zig index 555e8ab..d848eb7 100644 --- a/clap/args.zig +++ b/clap/args.zig @@ -252,7 +252,12 @@ pub const ShellIterator = struct { } } - fn result(iter: *ShellIterator, start: usize, end: usize, list: *std.ArrayList(u8)) Error!?[]const u8 { + fn result( + iter: *ShellIterator, + start: usize, + end: usize, + list: *std.ArrayList(u8), + ) Error!?[]const u8 { const res = iter.str[start..end]; // If we already have something in `list` that means that we could not diff --git a/clap/comptime.zig b/clap/comptime.zig index 7808f52..cbc872e 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig @@ -45,9 +45,8 @@ pub fn ComptimeClap( pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() { const allocator = opt.allocator; var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; - for (multis) |*multi| { + for (multis) |*multi| multi.* = std.ArrayList([]const u8).init(allocator); - } var pos = std.ArrayList([]const u8).init(allocator); diff --git a/clap/streaming.zig b/clap/streaming.zig index e2f1311..885c581 100644 --- a/clap/streaming.zig +++ b/clap/streaming.zig @@ -21,8 +21,8 @@ pub fn Arg(comptime Id: type) type { } /// A command line argument parser which, given an ArgIterator, will parse arguments according -/// to the params. StreamingClap parses in an iterating manner, so you have to use a loop together with -/// StreamingClap.next to parse all the arguments of your program. +/// to the params. StreamingClap parses in an iterating manner, so you have to use a loop +/// together with StreamingClap.next to parse all the arguments of your program. pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { return struct { const State = union(enum) { @@ -203,7 +203,11 @@ 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, @@ -225,7 +229,11 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r return error.TestFailed; } -fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) !void { +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){ @@ -420,5 +428,9 @@ test "errors" { 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"); + try testErr( + ¶ms, + &.{"--cc"}, + "The argument '--cc' requires a value but none was supplied\n", + ); }