summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--clap/parsers.zig12
1 files changed, 10 insertions, 2 deletions
diff --git a/clap/parsers.zig b/clap/parsers.zig
index 208a1aa..e6d3bdb 100644
--- a/clap/parsers.zig
+++ b/clap/parsers.zig
@@ -20,6 +20,7 @@ pub const default = .{
20 .f64 = float(f64), 20 .f64 = float(f64),
21}; 21};
22 22
23/// A parser that does nothing.
23pub fn string(in: []const u8) error{}![]const u8 { 24pub fn string(in: []const u8) error{}![]const u8 {
24 return in; 25 return in;
25} 26}
@@ -28,10 +29,12 @@ test "string" {
28 try testing.expectEqualStrings("aa", try string("aa")); 29 try testing.expectEqualStrings("aa", try string("aa"));
29} 30}
30 31
31pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntError!T { 32/// A parser that uses `std.fmt.parseInt` to parse the string into an integer value.
33/// See `std.fmt.parseInt` documentation for more information.
34pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) fmt.ParseIntError!T {
32 return struct { 35 return struct {
33 fn parse(in: []const u8) fmt.ParseIntError!T { 36 fn parse(in: []const u8) fmt.ParseIntError!T {
34 return fmt.parseInt(T, in, radix); 37 return fmt.parseInt(T, in, base);
35 } 38 }
36 }.parse; 39 }.parse;
37} 40}
@@ -44,6 +47,8 @@ test "int" {
44 try testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10")); 47 try testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10"));
45} 48}
46 49
50/// A parser that uses `std.fmt.parseFloat` to parse the string into an float value.
51/// See `std.fmt.parseFloat` documentation for more information.
47pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { 52pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T {
48 return struct { 53 return struct {
49 fn parse(in: []const u8) fmt.ParseFloatError!T { 54 fn parse(in: []const u8) fmt.ParseFloatError!T {
@@ -60,6 +65,9 @@ pub const EnumError = error{
60 NameNotPartOfEnum, 65 NameNotPartOfEnum,
61}; 66};
62 67
68/// A parser that uses `std.meta.stringToEnum` to parse the string into an enum value. On `null`,
69/// this function returns the error `NameNotPartOfEnum`.
70/// See `std.meta.stringToEnum` documentation for more information.
63pub fn enumeration(comptime T: type) fn ([]const u8) EnumError!T { 71pub fn enumeration(comptime T: type) fn ([]const u8) EnumError!T {
64 return struct { 72 return struct {
65 fn parse(in: []const u8) EnumError!T { 73 fn parse(in: []const u8) EnumError!T {