diff options
Diffstat (limited to 'clap/parsers.zig')
| -rw-r--r-- | clap/parsers.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/clap/parsers.zig b/clap/parsers.zig new file mode 100644 index 0000000..49b95a9 --- /dev/null +++ b/clap/parsers.zig | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const fmt = std.fmt; | ||
| 4 | |||
| 5 | pub const default = .{ | ||
| 6 | .string = string, | ||
| 7 | .str = string, | ||
| 8 | .u8 = int(u8, 0), | ||
| 9 | .u16 = int(u16, 0), | ||
| 10 | .u32 = int(u32, 0), | ||
| 11 | .u64 = int(u64, 0), | ||
| 12 | .usize = int(usize, 0), | ||
| 13 | .i8 = int(i8, 0), | ||
| 14 | .i16 = int(i16, 0), | ||
| 15 | .i32 = int(i32, 0), | ||
| 16 | .i64 = int(i64, 0), | ||
| 17 | .isize = int(isize, 0), | ||
| 18 | .f32 = float(f32), | ||
| 19 | .f64 = float(f64), | ||
| 20 | }; | ||
| 21 | |||
| 22 | pub fn string(in: []const u8) error{}![]const u8 { | ||
| 23 | return in; | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntError!T { | ||
| 27 | return struct { | ||
| 28 | fn parse(in: []const u8) fmt.ParseIntError!T { | ||
| 29 | return fmt.parseInt(T, in, radix); | ||
| 30 | } | ||
| 31 | }.parse; | ||
| 32 | } | ||
| 33 | |||
| 34 | pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { | ||
| 35 | return struct { | ||
| 36 | fn parse(in: []const u8) fmt.ParseFloatError!T { | ||
| 37 | return fmt.parseFloat(T, in); | ||
| 38 | } | ||
| 39 | }.parse; | ||
| 40 | } | ||
| 41 | |||
| 42 | fn ReturnType(comptime P: type) type { | ||
| 43 | return @typeInfo(P).Fn.return_type.?; | ||
| 44 | } | ||
| 45 | |||
| 46 | pub fn Result(comptime P: type) type { | ||
| 47 | return @typeInfo(ReturnType(P)).ErrorUnion.payload; | ||
| 48 | } | ||