summaryrefslogtreecommitdiff
path: root/src/extended.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/extended.zig51
1 files changed, 21 insertions, 30 deletions
diff --git a/src/extended.zig b/src/extended.zig
index ec8310e..09e91dd 100644
--- a/src/extended.zig
+++ b/src/extended.zig
@@ -1,12 +1,12 @@
1pub const core = @import("core.zig"); 1pub const core = @import("core.zig");
2 2
3const builtin = @import("builtin"); 3const builtin = @import("builtin");
4const std = @import("std"); 4const std = @import("std");
5 5
6const mem = std.mem; 6const mem = std.mem;
7const fmt = std.fmt; 7const fmt = std.fmt;
8const debug = std.debug; 8const debug = std.debug;
9const io = std.io; 9const io = std.io;
10 10
11const assert = debug.assert; 11const assert = debug.assert;
12 12
@@ -92,7 +92,7 @@ pub const Parser = struct {
92 func: UnsafeFunction, 92 func: UnsafeFunction,
93 93
94 pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser { 94 pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser {
95 return Parser { 95 return Parser{
96 .FieldType = FieldType, 96 .FieldType = FieldType,
97 .Errors = Errors, 97 .Errors = Errors,
98 .func = @ptrCast(UnsafeFunction, func), 98 .func = @ptrCast(UnsafeFunction, func),
@@ -104,7 +104,7 @@ pub const Parser = struct {
104 } 104 }
105 105
106 fn ParseFunc(comptime FieldType: type, comptime Errors: type) type { 106 fn ParseFunc(comptime FieldType: type, comptime Errors: type) type {
107 return fn(*FieldType, []const u8) Errors!void; 107 return fn (*FieldType, []const u8) Errors!void;
108 } 108 }
109 109
110 pub fn int(comptime Int: type, comptime radix: u8) Parser { 110 pub fn int(comptime Int: type, comptime radix: u8) Parser {
@@ -113,22 +113,14 @@ pub const Parser = struct {
113 field_ptr.* = try fmt.parseInt(Int, arg, radix); 113 field_ptr.* = try fmt.parseInt(Int, arg, radix);
114 } 114 }
115 }.i; 115 }.i;
116 return Parser.init( 116 return Parser.init(Int, @typeOf(func).ReturnType.ErrorSet, func);
117 Int,
118 @typeOf(func).ReturnType.ErrorSet,
119 func
120 );
121 } 117 }
122 118
123 const string = Parser.init( 119 const string = Parser.init([]const u8, error{}, struct {
124 []const u8, 120 fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) {
125 error{}, 121 field_ptr.* = arg;
126 struct { 122 }
127 fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { 123 }.s);
128 field_ptr.* = arg;
129 }
130 }.s
131 );
132}; 124};
133 125
134pub fn Clap(comptime Result: type) type { 126pub fn Clap(comptime Result: type) type {
@@ -174,7 +166,7 @@ pub fn Clap(comptime Result: type) type {
174 166
175 for (command.params) |p, i| { 167 for (command.params) |p, i| {
176 const id = i; 168 const id = i;
177 res[id] = core.Param(usize) { 169 res[id] = core.Param(usize){
178 .id = id, 170 .id = id,
179 .takes_value = p.kind == Param.Kind.Option, 171 .takes_value = p.kind == Param.Kind.Option,
180 .names = p.names, 172 .names = p.names,
@@ -186,10 +178,9 @@ pub fn Clap(comptime Result: type) type {
186 178
187 var pos: usize = 0; 179 var pos: usize = 0;
188 180
189 arg_loop: 181 arg_loop: while (try clap.next()) |arg| : (pos += 1) {
190 while (try clap.next()) |arg| : (pos += 1) { 182 inline for (command.params) |param, i| {
191 inline for(command.params) |param, i| { 183 if (arg.param.id == i and (param.settings.position orelse pos) == pos) {
192 if (arg.param.id == i and (param.settings.position ?? pos) == pos) {
193 handled[i] = true; 184 handled[i] = true;
194 185
195 switch (param.kind) { 186 switch (param.kind) {
@@ -197,7 +188,7 @@ pub fn Clap(comptime Result: type) type {
197 getFieldPtr(&result, param.field).* = true; 188 getFieldPtr(&result, param.field).* = true;
198 }, 189 },
199 Param.Kind.Option => |parser| { 190 Param.Kind.Option => |parser| {
200 try parser.parse(getFieldPtr(&result, param.field), ??arg.value); 191 try parser.parse(getFieldPtr(&result, param.field), arg.value.?);
201 }, 192 },
202 Param.Kind.Subcommand => |sub_command| { 193 Param.Kind.Subcommand => |sub_command| {
203 getFieldPtr(&result, param.field).* = try sub_command.parseHelper(Error, clap); 194 getFieldPtr(&result, param.field).* = try sub_command.parseHelper(Error, clap);
@@ -223,19 +214,19 @@ pub fn Clap(comptime Result: type) type {
223 214
224 fn GetFieldPtrReturn(comptime Struct: type, comptime field: []const u8) type { 215 fn GetFieldPtrReturn(comptime Struct: type, comptime field: []const u8) type {
225 var inst: Struct = undefined; 216 var inst: Struct = undefined;
226 const dot_index = comptime mem.indexOfScalar(u8, field, '.') ?? { 217 const dot_index = comptime mem.indexOfScalar(u8, field, '.') orelse {
227 return @typeOf(&@field(inst, field)); 218 return @typeOf(&@field(inst, field));
228 }; 219 };
229 220
230 return GetFieldPtrReturn(@typeOf(@field(inst, field[0..dot_index])), field[dot_index + 1..]); 221 return GetFieldPtrReturn(@typeOf(@field(inst, field[0..dot_index])), field[dot_index + 1 ..]);
231 } 222 }
232 223
233 fn getFieldPtr(curr: var, comptime field: []const u8) GetFieldPtrReturn(@typeOf(curr).Child, field) { 224 fn getFieldPtr(curr: var, comptime field: []const u8) GetFieldPtrReturn(@typeOf(curr).Child, field) {
234 const dot_index = comptime mem.indexOfScalar(u8, field, '.') ?? { 225 const dot_index = comptime mem.indexOfScalar(u8, field, '.') orelse {
235 return &@field(curr, field); 226 return &@field(curr, field);
236 }; 227 };
237 228
238 return getFieldPtr(&@field(curr, field[0..dot_index]), field[dot_index + 1..]); 229 return getFieldPtr(&@field(curr, field[0..dot_index]), field[dot_index + 1 ..]);
239 } 230 }
240 }; 231 };
241} 232}