diff options
| author | 2022-07-25 18:15:45 +0200 | |
|---|---|---|
| committer | 2022-07-25 18:16:19 +0200 | |
| commit | 7f9eabeecfde0d909ab50a8fb10ad3fedff93d03 (patch) | |
| tree | 0e7ee4f043ef215524ec41062e2442cd00615270 | |
| parent | Update LICENSE (diff) | |
| download | zig-clap-7f9eabeecfde0d909ab50a8fb10ad3fedff93d03.tar.gz zig-clap-7f9eabeecfde0d909ab50a8fb10ad3fedff93d03.tar.xz zig-clap-7f9eabeecfde0d909ab50a8fb10ad3fedff93d03.zip | |
Add clap.parsers.enumeration for parsing enums
closes #78
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | clap/parsers.zig | 37 | ||||
| -rw-r--r-- | example/simple-ex.zig | 5 |
3 files changed, 47 insertions, 0 deletions
| @@ -96,6 +96,7 @@ pub fn main() !void { | |||
| 96 | const params = comptime clap.parseParamsComptime( | 96 | const params = comptime clap.parseParamsComptime( |
| 97 | \\-h, --help Display this help and exit. | 97 | \\-h, --help Display this help and exit. |
| 98 | \\-n, --number <INT> An option parameter, which takes a value. | 98 | \\-n, --number <INT> An option parameter, which takes a value. |
| 99 | \\-a, --answer <ANSWER> An option parameter which takes an enum. | ||
| 99 | \\-s, --string <STR>... An option parameter which can be specified multiple times. | 100 | \\-s, --string <STR>... An option parameter which can be specified multiple times. |
| 100 | \\<FILE>... | 101 | \\<FILE>... |
| 101 | \\ | 102 | \\ |
| @@ -103,10 +104,12 @@ pub fn main() !void { | |||
| 103 | 104 | ||
| 104 | // Declare our own parsers which are used to map the argument strings to other | 105 | // Declare our own parsers which are used to map the argument strings to other |
| 105 | // types. | 106 | // types. |
| 107 | const YesNo = enum { yes, no }; | ||
| 106 | const parsers = comptime .{ | 108 | const parsers = comptime .{ |
| 107 | .STR = clap.parsers.string, | 109 | .STR = clap.parsers.string, |
| 108 | .FILE = clap.parsers.string, | 110 | .FILE = clap.parsers.string, |
| 109 | .INT = clap.parsers.int(usize, 10), | 111 | .INT = clap.parsers.int(usize, 10), |
| 112 | .ANSWER = clap.parsers.enumeration(YesNo), | ||
| 110 | }; | 113 | }; |
| 111 | 114 | ||
| 112 | var diag = clap.Diagnostic{}; | 115 | var diag = clap.Diagnostic{}; |
| @@ -122,6 +125,8 @@ pub fn main() !void { | |||
| 122 | debug.print("--help\n", .{}); | 125 | debug.print("--help\n", .{}); |
| 123 | if (res.args.number) |n| | 126 | if (res.args.number) |n| |
| 124 | debug.print("--number = {}\n", .{n}); | 127 | debug.print("--number = {}\n", .{n}); |
| 128 | if (res.args.answer) |a| | ||
| 129 | debug.print("--answer = {s}\n", .{@tagName(a)}); | ||
| 125 | for (res.args.string) |s| | 130 | for (res.args.string) |s| |
| 126 | debug.print("--string = {s}\n", .{s}); | 131 | debug.print("--string = {s}\n", .{s}); |
| 127 | for (res.positionals) |pos| | 132 | for (res.positionals) |pos| |
diff --git a/clap/parsers.zig b/clap/parsers.zig index 49b95a9..208a1aa 100644 --- a/clap/parsers.zig +++ b/clap/parsers.zig | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | 2 | ||
| 3 | const fmt = std.fmt; | 3 | const fmt = std.fmt; |
| 4 | const testing = std.testing; | ||
| 4 | 5 | ||
| 5 | pub const default = .{ | 6 | pub const default = .{ |
| 6 | .string = string, | 7 | .string = string, |
| @@ -23,6 +24,10 @@ pub fn string(in: []const u8) error{}![]const u8 { | |||
| 23 | return in; | 24 | return in; |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 27 | test "string" { | ||
| 28 | try testing.expectEqualStrings("aa", try string("aa")); | ||
| 29 | } | ||
| 30 | |||
| 26 | pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntError!T { | 31 | pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntError!T { |
| 27 | return struct { | 32 | return struct { |
| 28 | fn parse(in: []const u8) fmt.ParseIntError!T { | 33 | fn parse(in: []const u8) fmt.ParseIntError!T { |
| @@ -31,6 +36,14 @@ pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntErr | |||
| 31 | }.parse; | 36 | }.parse; |
| 32 | } | 37 | } |
| 33 | 38 | ||
| 39 | test "int" { | ||
| 40 | try testing.expectEqual(@as(u8, 0), try int(u8, 10)("0")); | ||
| 41 | try testing.expectEqual(@as(u8, 1), try int(u8, 10)("1")); | ||
| 42 | try testing.expectEqual(@as(u8, 10), try int(u8, 10)("10")); | ||
| 43 | try testing.expectEqual(@as(u8, 0x10), try int(u8, 0)("0x10")); | ||
| 44 | try testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10")); | ||
| 45 | } | ||
| 46 | |||
| 34 | pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { | 47 | pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { |
| 35 | return struct { | 48 | return struct { |
| 36 | fn parse(in: []const u8) fmt.ParseFloatError!T { | 49 | fn parse(in: []const u8) fmt.ParseFloatError!T { |
| @@ -39,6 +52,30 @@ pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { | |||
| 39 | }.parse; | 52 | }.parse; |
| 40 | } | 53 | } |
| 41 | 54 | ||
| 55 | test "float" { | ||
| 56 | try testing.expectEqual(@as(f32, 0), try float(f32)("0")); | ||
| 57 | } | ||
| 58 | |||
| 59 | pub const EnumError = error{ | ||
| 60 | NameNotPartOfEnum, | ||
| 61 | }; | ||
| 62 | |||
| 63 | pub fn enumeration(comptime T: type) fn ([]const u8) EnumError!T { | ||
| 64 | return struct { | ||
| 65 | fn parse(in: []const u8) EnumError!T { | ||
| 66 | return std.meta.stringToEnum(T, in) orelse error.NameNotPartOfEnum; | ||
| 67 | } | ||
| 68 | }.parse; | ||
| 69 | } | ||
| 70 | |||
| 71 | test "enumeration" { | ||
| 72 | const E = enum { a, b, c }; | ||
| 73 | try testing.expectEqual(E.a, try enumeration(E)("a")); | ||
| 74 | try testing.expectEqual(E.b, try enumeration(E)("b")); | ||
| 75 | try testing.expectEqual(E.c, try enumeration(E)("c")); | ||
| 76 | try testing.expectError(EnumError.NameNotPartOfEnum, enumeration(E)("d")); | ||
| 77 | } | ||
| 78 | |||
| 42 | fn ReturnType(comptime P: type) type { | 79 | fn ReturnType(comptime P: type) type { |
| 43 | return @typeInfo(P).Fn.return_type.?; | 80 | return @typeInfo(P).Fn.return_type.?; |
| 44 | } | 81 | } |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d0d214d..fb20d07 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -11,6 +11,7 @@ pub fn main() !void { | |||
| 11 | const params = comptime clap.parseParamsComptime( | 11 | const params = comptime clap.parseParamsComptime( |
| 12 | \\-h, --help Display this help and exit. | 12 | \\-h, --help Display this help and exit. |
| 13 | \\-n, --number <INT> An option parameter, which takes a value. | 13 | \\-n, --number <INT> An option parameter, which takes a value. |
| 14 | \\-a, --answer <ANSWER> An option parameter which takes an enum. | ||
| 14 | \\-s, --string <STR>... An option parameter which can be specified multiple times. | 15 | \\-s, --string <STR>... An option parameter which can be specified multiple times. |
| 15 | \\<FILE>... | 16 | \\<FILE>... |
| 16 | \\ | 17 | \\ |
| @@ -18,10 +19,12 @@ pub fn main() !void { | |||
| 18 | 19 | ||
| 19 | // Declare our own parsers which are used to map the argument strings to other | 20 | // Declare our own parsers which are used to map the argument strings to other |
| 20 | // types. | 21 | // types. |
| 22 | const YesNo = enum { yes, no }; | ||
| 21 | const parsers = comptime .{ | 23 | const parsers = comptime .{ |
| 22 | .STR = clap.parsers.string, | 24 | .STR = clap.parsers.string, |
| 23 | .FILE = clap.parsers.string, | 25 | .FILE = clap.parsers.string, |
| 24 | .INT = clap.parsers.int(usize, 10), | 26 | .INT = clap.parsers.int(usize, 10), |
| 27 | .ANSWER = clap.parsers.enumeration(YesNo), | ||
| 25 | }; | 28 | }; |
| 26 | 29 | ||
| 27 | var diag = clap.Diagnostic{}; | 30 | var diag = clap.Diagnostic{}; |
| @@ -37,6 +40,8 @@ pub fn main() !void { | |||
| 37 | debug.print("--help\n", .{}); | 40 | debug.print("--help\n", .{}); |
| 38 | if (res.args.number) |n| | 41 | if (res.args.number) |n| |
| 39 | debug.print("--number = {}\n", .{n}); | 42 | debug.print("--number = {}\n", .{n}); |
| 43 | if (res.args.answer) |a| | ||
| 44 | debug.print("--answer = {s}\n", .{@tagName(a)}); | ||
| 40 | for (res.args.string) |s| | 45 | for (res.args.string) |s| |
| 41 | debug.print("--string = {s}\n", .{s}); | 46 | debug.print("--string = {s}\n", .{s}); |
| 42 | for (res.positionals) |pos| | 47 | for (res.positionals) |pos| |