diff options
Diffstat (limited to 'clap.zig')
| -rw-r--r-- | clap.zig | 120 |
1 files changed, 87 insertions, 33 deletions
| @@ -43,8 +43,8 @@ pub const Values = enum { | |||
| 43 | /// * "-abc value" | 43 | /// * "-abc value" |
| 44 | /// * "-abc=value" | 44 | /// * "-abc=value" |
| 45 | /// * "-abcvalue" | 45 | /// * "-abcvalue" |
| 46 | /// * Long ("--long-param"): Should be used for less common parameters, or when no single character | 46 | /// * Long ("--long-param"): Should be used for less common parameters, or when no single |
| 47 | /// can describe the paramter. | 47 | /// character can describe the paramter. |
| 48 | /// * They can take a value two different ways. | 48 | /// * They can take a value two different ways. |
| 49 | /// * "--long-param value" | 49 | /// * "--long-param value" |
| 50 | /// * "--long-param=value" | 50 | /// * "--long-param=value" |
| @@ -229,9 +229,18 @@ pub const Diagnostic = struct { | |||
| 229 | Arg{ .prefix = "", .name = diag.arg }; | 229 | Arg{ .prefix = "", .name = diag.arg }; |
| 230 | 230 | ||
| 231 | switch (err) { | 231 | switch (err) { |
| 232 | error.DoesntTakeValue => try stream.print("The argument '{s}{s}' does not take a value\n", .{ a.prefix, a.name }), | 232 | error.DoesntTakeValue => try stream.print( |
| 233 | error.MissingValue => try stream.print("The argument '{s}{s}' requires a value but none was supplied\n", .{ a.prefix, a.name }), | 233 | "The argument '{s}{s}' does not take a value\n", |
| 234 | error.InvalidArgument => try stream.print("Invalid argument '{s}{s}'\n", .{ a.prefix, a.name }), | 234 | .{ a.prefix, a.name }, |
| 235 | ), | ||
| 236 | error.MissingValue => try stream.print( | ||
| 237 | "The argument '{s}{s}' requires a value but none was supplied\n", | ||
| 238 | .{ a.prefix, a.name }, | ||
| 239 | ), | ||
| 240 | error.InvalidArgument => try stream.print( | ||
| 241 | "Invalid argument '{s}{s}'\n", | ||
| 242 | .{ a.prefix, a.name }, | ||
| 243 | ), | ||
| 235 | else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}), | 244 | else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}), |
| 236 | } | 245 | } |
| 237 | } | 246 | } |
| @@ -246,15 +255,51 @@ fn testDiag(diag: Diagnostic, err: anyerror, expected: []const u8) !void { | |||
| 246 | 255 | ||
| 247 | test "Diagnostic.report" { | 256 | test "Diagnostic.report" { |
| 248 | try testDiag(.{ .arg = "c" }, error.InvalidArgument, "Invalid argument 'c'\n"); | 257 | try testDiag(.{ .arg = "c" }, error.InvalidArgument, "Invalid argument 'c'\n"); |
| 249 | try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n"); | 258 | try testDiag( |
| 250 | try testDiag(.{ .name = .{ .short = 'c' } }, error.DoesntTakeValue, "The argument '-c' does not take a value\n"); | 259 | .{ .name = .{ .long = "cc" } }, |
| 251 | try testDiag(.{ .name = .{ .long = "cc" } }, error.DoesntTakeValue, "The argument '--cc' does not take a value\n"); | 260 | error.InvalidArgument, |
| 252 | try testDiag(.{ .name = .{ .short = 'c' } }, error.MissingValue, "The argument '-c' requires a value but none was supplied\n"); | 261 | "Invalid argument '--cc'\n", |
| 253 | try testDiag(.{ .name = .{ .long = "cc" } }, error.MissingValue, "The argument '--cc' requires a value but none was supplied\n"); | 262 | ); |
| 254 | try testDiag(.{ .name = .{ .short = 'c' } }, error.InvalidArgument, "Invalid argument '-c'\n"); | 263 | try testDiag( |
| 255 | try testDiag(.{ .name = .{ .long = "cc" } }, error.InvalidArgument, "Invalid argument '--cc'\n"); | 264 | .{ .name = .{ .short = 'c' } }, |
| 256 | try testDiag(.{ .name = .{ .short = 'c' } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n"); | 265 | error.DoesntTakeValue, |
| 257 | try testDiag(.{ .name = .{ .long = "cc" } }, error.SomethingElse, "Error while parsing arguments: SomethingElse\n"); | 266 | "The argument '-c' does not take a value\n", |
| 267 | ); | ||
| 268 | try testDiag( | ||
| 269 | .{ .name = .{ .long = "cc" } }, | ||
| 270 | error.DoesntTakeValue, | ||
| 271 | "The argument '--cc' does not take a value\n", | ||
| 272 | ); | ||
| 273 | try testDiag( | ||
| 274 | .{ .name = .{ .short = 'c' } }, | ||
| 275 | error.MissingValue, | ||
| 276 | "The argument '-c' requires a value but none was supplied\n", | ||
| 277 | ); | ||
| 278 | try testDiag( | ||
| 279 | .{ .name = .{ .long = "cc" } }, | ||
| 280 | error.MissingValue, | ||
| 281 | "The argument '--cc' requires a value but none was supplied\n", | ||
| 282 | ); | ||
| 283 | try testDiag( | ||
| 284 | .{ .name = .{ .short = 'c' } }, | ||
| 285 | error.InvalidArgument, | ||
| 286 | "Invalid argument '-c'\n", | ||
| 287 | ); | ||
| 288 | try testDiag( | ||
| 289 | .{ .name = .{ .long = "cc" } }, | ||
| 290 | error.InvalidArgument, | ||
| 291 | "Invalid argument '--cc'\n", | ||
| 292 | ); | ||
| 293 | try testDiag( | ||
| 294 | .{ .name = .{ .short = 'c' } }, | ||
| 295 | error.SomethingElse, | ||
| 296 | "Error while parsing arguments: SomethingElse\n", | ||
| 297 | ); | ||
| 298 | try testDiag( | ||
| 299 | .{ .name = .{ .long = "cc" } }, | ||
| 300 | error.SomethingElse, | ||
| 301 | "Error while parsing arguments: SomethingElse\n", | ||
| 302 | ); | ||
| 258 | } | 303 | } |
| 259 | 304 | ||
| 260 | pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | 305 | pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { |
| @@ -466,7 +511,9 @@ test "clap.help" { | |||
| 466 | parseParam("-c, --cc Both flag.") catch unreachable, | 511 | parseParam("-c, --cc Both flag.") catch unreachable, |
| 467 | parseParam("-d, --dd <V3> Both option.") catch unreachable, | 512 | parseParam("-d, --dd <V3> Both option.") catch unreachable, |
| 468 | parseParam("-d, --dd <V3>... Both repeated option.") catch unreachable, | 513 | parseParam("-d, --dd <V3>... Both repeated option.") catch unreachable, |
| 469 | parseParam("<P> Positional. This should not appear in the help message.") catch unreachable, | 514 | parseParam( |
| 515 | "<P> Positional. This should not appear in the help message.", | ||
| 516 | ) catch unreachable, | ||
| 470 | }, | 517 | }, |
| 471 | ); | 518 | ); |
| 472 | 519 | ||
| @@ -516,12 +563,16 @@ pub fn usageFull( | |||
| 516 | 563 | ||
| 517 | const prefix = if (param.names.short) |_| "-" else "--"; | 564 | const prefix = if (param.names.short) |_| "-" else "--"; |
| 518 | 565 | ||
| 519 | // Seems the zig compiler is being a little wierd. I doesn't allow me to write | 566 | const name = if (param.names.short) |*s| |
| 520 | // @as(*const [1]u8, s) VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV | 567 | // Seems the zig compiler is being a little wierd. I doesn't allow me to write |
| 521 | const name = if (param.names.short) |*s| @ptrCast([*]const u8, s)[0..1] else param.names.long orelse { | 568 | // @as(*const [1]u8, s) |
| 522 | positional = param; | 569 | @ptrCast([*]const u8, s)[0..1] |
| 523 | continue; | 570 | else |
| 524 | }; | 571 | param.names.long orelse { |
| 572 | positional = param; | ||
| 573 | continue; | ||
| 574 | }; | ||
| 575 | |||
| 525 | if (cos.bytes_written != 0) | 576 | if (cos.bytes_written != 0) |
| 526 | try cs.writeByte(' '); | 577 | try cs.writeByte(' '); |
| 527 | 578 | ||
| @@ -601,16 +652,19 @@ test "usage" { | |||
| 601 | try testUsage("<file>", &.{ | 652 | try testUsage("<file>", &.{ |
| 602 | try parseParam("<file>"), | 653 | try parseParam("<file>"), |
| 603 | }); | 654 | }); |
| 604 | try testUsage("[-ab] [-c <value>] [-d <v>] [--e] [--f] [--g <value>] [--h <v>] [-i <v>...] <file>", &.{ | 655 | try testUsage( |
| 605 | try parseParam("-a"), | 656 | "[-ab] [-c <value>] [-d <v>] [--e] [--f] [--g <value>] [--h <v>] [-i <v>...] <file>", |
| 606 | try parseParam("-b"), | 657 | &.{ |
| 607 | try parseParam("-c <value>"), | 658 | try parseParam("-a"), |
| 608 | try parseParam("-d <v>"), | 659 | try parseParam("-b"), |
| 609 | try parseParam("--e"), | 660 | try parseParam("-c <value>"), |
| 610 | try parseParam("--f"), | 661 | try parseParam("-d <v>"), |
| 611 | try parseParam("--g <value>"), | 662 | try parseParam("--e"), |
| 612 | try parseParam("--h <v>"), | 663 | try parseParam("--f"), |
| 613 | try parseParam("-i <v>..."), | 664 | try parseParam("--g <value>"), |
| 614 | try parseParam("<file>"), | 665 | try parseParam("--h <v>"), |
| 615 | }); | 666 | try parseParam("-i <v>..."), |
| 667 | try parseParam("<file>"), | ||
| 668 | }, | ||
| 669 | ); | ||
| 616 | } | 670 | } |