summaryrefslogtreecommitdiff
path: root/clap/parsers.zig
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
committerGravatar Komari Spaghetti2022-03-09 18:12:40 +0100
commit5f7b75d5523d9581eca5a72a362868ff517992e8 (patch)
tree5e874f9c935e0d7c838ea5aadf270ce2929f8e8a /clap/parsers.zig
parentBump actions/checkout from 2.4.0 to 3 (diff)
downloadzig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.gz
zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.xz
zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.zip
Allow for clap to parse argument values into types
This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access on the result of `parse` and `parseEx`. - `clap.ComptimeClap` has been removed. - `clap.StreamingClap` is now called `clap.streaming.Clap` - `parse` and `parseEx` now takes a `value_parsers` argument that provides the parsers to parse values. - Remove `helpEx`, `helpFull`, `usageEx` and `usageFull`. They now just expect `Id` to have methods for getting the description and value texts.
Diffstat (limited to 'clap/parsers.zig')
-rw-r--r--clap/parsers.zig48
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 @@
1const std = @import("std");
2
3const fmt = std.fmt;
4
5pub 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
22pub fn string(in: []const u8) error{}![]const u8 {
23 return in;
24}
25
26pub 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
34pub 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
42fn ReturnType(comptime P: type) type {
43 return @typeInfo(P).Fn.return_type.?;
44}
45
46pub fn Result(comptime P: type) type {
47 return @typeInfo(ReturnType(P)).ErrorUnion.payload;
48}