diff options
| -rw-r--r-- | README.md | 9 | ||||
| -rw-r--r-- | clap.zig | 26 | ||||
| -rw-r--r-- | example/README.md.template | 1 | ||||
| -rw-r--r-- | example/help.zig | 2 | ||||
| -rw-r--r-- | example/simple-ex.zig | 2 | ||||
| -rw-r--r-- | example/simple.zig | 2 | ||||
| -rw-r--r-- | example/usage.zig | 2 |
7 files changed, 31 insertions, 13 deletions
| @@ -14,6 +14,7 @@ in the release notes. | |||
| 14 | 14 | ||
| 15 | * Short arguments `-a` | 15 | * Short arguments `-a` |
| 16 | * Chaining `-abc` where `a` and `b` does not take values. | 16 | * Chaining `-abc` where `a` and `b` does not take values. |
| 17 | * Multiple specifications are tallied (e.g. `-v -v`). | ||
| 17 | * Long arguments `--long` | 18 | * Long arguments `--long` |
| 18 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | 19 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 19 | * Short args also support passing values with no spacing or `=` (`-a100`) | 20 | * Short args also support passing values with no spacing or `=` (`-a100`) |
| @@ -59,7 +60,7 @@ pub fn main() !void { | |||
| 59 | }; | 60 | }; |
| 60 | defer res.deinit(); | 61 | defer res.deinit(); |
| 61 | 62 | ||
| 62 | if (res.args.help) | 63 | if (res.args.help != 0) |
| 63 | debug.print("--help\n", .{}); | 64 | debug.print("--help\n", .{}); |
| 64 | if (res.args.number) |n| | 65 | if (res.args.number) |n| |
| 65 | debug.print("--number = {}\n", .{n}); | 66 | debug.print("--number = {}\n", .{n}); |
| @@ -121,7 +122,7 @@ pub fn main() !void { | |||
| 121 | }; | 122 | }; |
| 122 | defer res.deinit(); | 123 | defer res.deinit(); |
| 123 | 124 | ||
| 124 | if (res.args.help) | 125 | if (res.args.help != 0) |
| 125 | debug.print("--help\n", .{}); | 126 | debug.print("--help\n", .{}); |
| 126 | if (res.args.number) |n| | 127 | if (res.args.number) |n| |
| 127 | debug.print("--number = {}\n", .{n}); | 128 | debug.print("--number = {}\n", .{n}); |
| @@ -231,7 +232,7 @@ pub fn main() !void { | |||
| 231 | // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). | 232 | // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). |
| 232 | // The last argument contains options as to how `help` should print those parameters. Using | 233 | // The last argument contains options as to how `help` should print those parameters. Using |
| 233 | // `.{}` means the default options. | 234 | // `.{}` means the default options. |
| 234 | if (res.args.help) | 235 | if (res.args.help != 0) |
| 235 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); | 236 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); |
| 236 | } | 237 | } |
| 237 | 238 | ||
| @@ -268,7 +269,7 @@ pub fn main() !void { | |||
| 268 | 269 | ||
| 269 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` | 270 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` |
| 270 | // where `Id` has a `value` method (`Param(Help)` is one such parameter). | 271 | // where `Id` has a `value` method (`Param(Help)` is one such parameter). |
| 271 | if (res.args.help) | 272 | if (res.args.help != 0) |
| 272 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); | 273 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 273 | } | 274 | } |
| 274 | 275 | ||
| @@ -803,7 +803,7 @@ fn parseArg( | |||
| 803 | const longest = comptime param.names.longest(); | 803 | const longest = comptime param.names.longest(); |
| 804 | switch (longest.kind) { | 804 | switch (longest.kind) { |
| 805 | .short, .long => switch (param.takes_value) { | 805 | .short, .long => switch (param.takes_value) { |
| 806 | .none => @field(arguments, longest.name) = true, | 806 | .none => @field(arguments, longest.name) +|= 1, |
| 807 | .one => @field(arguments, longest.name) = try parser(arg.value.?), | 807 | .one => @field(arguments, longest.name) = try parser(arg.value.?), |
| 808 | .many => { | 808 | .many => { |
| 809 | const value = try parser(arg.value.?); | 809 | const value = try parser(arg.value.?); |
| @@ -912,7 +912,7 @@ fn Arguments( | |||
| 912 | 912 | ||
| 913 | const T = ParamType(Id, param, value_parsers); | 913 | const T = ParamType(Id, param, value_parsers); |
| 914 | const default_value = switch (param.takes_value) { | 914 | const default_value = switch (param.takes_value) { |
| 915 | .none => false, | 915 | .none => @as(u8, 0), |
| 916 | .one => @as(?T, null), | 916 | .one => @as(?T, null), |
| 917 | .many => switch (multi_arg_kind) { | 917 | .many => switch (multi_arg_kind) { |
| 918 | .slice => @as([]const T, &[_]T{}), | 918 | .slice => @as([]const T, &[_]T{}), |
| @@ -965,21 +965,37 @@ test "everything" { | |||
| 965 | ); | 965 | ); |
| 966 | 966 | ||
| 967 | var iter = args.SliceIterator{ | 967 | var iter = args.SliceIterator{ |
| 968 | .args = &.{ "-a", "-c", "0", "something", "-d", "1", "--dd", "2" }, | 968 | .args = &.{ "-a", "--aa", "-c", "0", "something", "-d", "1", "--dd", "2" }, |
| 969 | }; | 969 | }; |
| 970 | var res = try parseEx(Help, ¶ms, parsers.default, &iter, .{ | 970 | var res = try parseEx(Help, ¶ms, parsers.default, &iter, .{ |
| 971 | .allocator = testing.allocator, | 971 | .allocator = testing.allocator, |
| 972 | }); | 972 | }); |
| 973 | defer res.deinit(); | 973 | defer res.deinit(); |
| 974 | 974 | ||
| 975 | try testing.expect(res.args.aa); | 975 | try testing.expect(res.args.aa == 2); |
| 976 | try testing.expect(!res.args.bb); | 976 | try testing.expect(res.args.bb == 0); |
| 977 | try testing.expectEqualStrings("0", res.args.cc.?); | 977 | try testing.expectEqualStrings("0", res.args.cc.?); |
| 978 | try testing.expectEqual(@as(usize, 1), res.positionals.len); | 978 | try testing.expectEqual(@as(usize, 1), res.positionals.len); |
| 979 | try testing.expectEqualStrings("something", res.positionals[0]); | 979 | try testing.expectEqualStrings("something", res.positionals[0]); |
| 980 | try testing.expectEqualSlices(usize, &.{ 1, 2 }, res.args.dd); | 980 | try testing.expectEqualSlices(usize, &.{ 1, 2 }, res.args.dd); |
| 981 | } | 981 | } |
| 982 | 982 | ||
| 983 | test "overflow-safe" { | ||
| 984 | const params = comptime parseParamsComptime( | ||
| 985 | \\-a, --aa | ||
| 986 | ); | ||
| 987 | |||
| 988 | var iter = args.SliceIterator{ | ||
| 989 | .args = &(.{"-" ++ ("a" ** 300)}), | ||
| 990 | }; | ||
| 991 | |||
| 992 | // This just needs to not crash | ||
| 993 | var res = try parseEx(Help, ¶ms, parsers.default, &iter, .{ | ||
| 994 | .allocator = testing.allocator, | ||
| 995 | }); | ||
| 996 | defer res.deinit(); | ||
| 997 | } | ||
| 998 | |||
| 983 | test "empty" { | 999 | test "empty" { |
| 984 | var iter = args.SliceIterator{ .args = &.{} }; | 1000 | var iter = args.SliceIterator{ .args = &.{} }; |
| 985 | var res = try parseEx(u8, &[_]Param(u8){}, parsers.default, &iter, .{ | 1001 | var res = try parseEx(u8, &[_]Param(u8){}, parsers.default, &iter, .{ |
diff --git a/example/README.md.template b/example/README.md.template index 696c598..34bff56 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -14,6 +14,7 @@ in the release notes. | |||
| 14 | 14 | ||
| 15 | * Short arguments `-a` | 15 | * Short arguments `-a` |
| 16 | * Chaining `-abc` where `a` and `b` does not take values. | 16 | * Chaining `-abc` where `a` and `b` does not take values. |
| 17 | * Multiple specifications are tallied (e.g. `-v -v`). | ||
| 17 | * Long arguments `--long` | 18 | * Long arguments `--long` |
| 18 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | 19 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 19 | * Short args also support passing values with no spacing or `=` (`-a100`) | 20 | * Short args also support passing values with no spacing or `=` (`-a100`) |
diff --git a/example/help.zig b/example/help.zig index dd9c9ea..e83ae44 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -15,6 +15,6 @@ pub fn main() !void { | |||
| 15 | // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). | 15 | // where `Id` has a `describtion` and `value` method (`Param(Help)` is one such parameter). |
| 16 | // The last argument contains options as to how `help` should print those parameters. Using | 16 | // The last argument contains options as to how `help` should print those parameters. Using |
| 17 | // `.{}` means the default options. | 17 | // `.{}` means the default options. |
| 18 | if (res.args.help) | 18 | if (res.args.help != 0) |
| 19 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); | 19 | return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); |
| 20 | } | 20 | } |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index fb20d07..dd5d929 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -36,7 +36,7 @@ pub fn main() !void { | |||
| 36 | }; | 36 | }; |
| 37 | defer res.deinit(); | 37 | defer res.deinit(); |
| 38 | 38 | ||
| 39 | if (res.args.help) | 39 | if (res.args.help != 0) |
| 40 | debug.print("--help\n", .{}); | 40 | debug.print("--help\n", .{}); |
| 41 | if (res.args.number) |n| | 41 | if (res.args.number) |n| |
| 42 | debug.print("--number = {}\n", .{n}); | 42 | debug.print("--number = {}\n", .{n}); |
diff --git a/example/simple.zig b/example/simple.zig index 2d32463..429f095 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -28,7 +28,7 @@ pub fn main() !void { | |||
| 28 | }; | 28 | }; |
| 29 | defer res.deinit(); | 29 | defer res.deinit(); |
| 30 | 30 | ||
| 31 | if (res.args.help) | 31 | if (res.args.help != 0) |
| 32 | debug.print("--help\n", .{}); | 32 | debug.print("--help\n", .{}); |
| 33 | if (res.args.number) |n| | 33 | if (res.args.number) |n| |
| 34 | debug.print("--number = {}\n", .{n}); | 34 | debug.print("--number = {}\n", .{n}); |
diff --git a/example/usage.zig b/example/usage.zig index 0d21352..333536b 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -14,6 +14,6 @@ pub fn main() !void { | |||
| 14 | 14 | ||
| 15 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` | 15 | // `clap.usage` is a function that can print a simple help message. It can print any `Param` |
| 16 | // where `Id` has a `value` method (`Param(Help)` is one such parameter). | 16 | // where `Id` has a `value` method (`Param(Help)` is one such parameter). |
| 17 | if (res.args.help) | 17 | if (res.args.help != 0) |
| 18 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); | 18 | return clap.usage(std.io.getStdErr().writer(), clap.Help, ¶ms); |
| 19 | } | 19 | } |