summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core.zig65
-rw-r--r--src/extended.zig51
2 files changed, 53 insertions, 63 deletions
diff --git a/src/core.zig b/src/core.zig
index d870e8c..a0f794c 100644
--- a/src/core.zig
+++ b/src/core.zig
@@ -102,12 +102,10 @@ pub fn Param(comptime Id: type) type {
102 pub fn init(id: Id, takes_value: bool, names: *const Names) Self { 102 pub fn init(id: Id, takes_value: bool, names: *const Names) Self {
103 // Assert, that if the param have no name, then it has to take 103 // Assert, that if the param have no name, then it has to take
104 // a value. 104 // a value.
105 debug.assert( 105 debug.assert(names.bare != null or
106 names.bare != null or
107 names.long != null or 106 names.long != null or
108 names.short != null or 107 names.short != null or
109 takes_value 108 takes_value);
110 );
111 109
112 return Self{ 110 return Self{
113 .id = id, 111 .id = id,
@@ -127,7 +125,7 @@ pub fn Arg(comptime Id: type) type {
127 value: ?[]const u8, 125 value: ?[]const u8,
128 126
129 pub fn init(param: *const Param(Id), value: ?[]const u8) Self { 127 pub fn init(param: *const Param(Id), value: ?[]const u8) Self {
130 return Self { 128 return Self{
131 .param = param, 129 .param = param,
132 .value = value, 130 .value = value,
133 }; 131 };
@@ -141,7 +139,7 @@ pub fn ArgIterator(comptime E: type) type {
141 const Self = this; 139 const Self = this;
142 const Error = E; 140 const Error = E;
143 141
144 nextFn: fn(iter: *Self) Error!?[]const u8, 142 nextFn: fn (iter: *Self) Error!?[]const u8,
145 143
146 pub fn next(iter: *Self) Error!?[]const u8 { 144 pub fn next(iter: *Self) Error!?[]const u8 {
147 return iter.nextFn(iter); 145 return iter.nextFn(iter);
@@ -159,12 +157,10 @@ pub const ArgSliceIterator = struct {
159 iter: ArgIterator(Error), 157 iter: ArgIterator(Error),
160 158
161 pub fn init(args: []const []const u8) ArgSliceIterator { 159 pub fn init(args: []const []const u8) ArgSliceIterator {
162 return ArgSliceIterator { 160 return ArgSliceIterator{
163 .args = args, 161 .args = args,
164 .index = 0, 162 .index = 0,
165 .iter = ArgIterator(Error) { 163 .iter = ArgIterator(Error){ .nextFn = nextFn },
166 .nextFn = nextFn,
167 },
168 }; 164 };
169 } 165 }
170 166
@@ -188,12 +184,10 @@ pub const OsArgIterator = struct {
188 iter: ArgIterator(Error), 184 iter: ArgIterator(Error),
189 185
190 pub fn init(allocator: *mem.Allocator) OsArgIterator { 186 pub fn init(allocator: *mem.Allocator) OsArgIterator {
191 return OsArgIterator { 187 return OsArgIterator{
192 .arena = heap.ArenaAllocator.init(allocator), 188 .arena = heap.ArenaAllocator.init(allocator),
193 .args = os.args(), 189 .args = os.args(),
194 .iter = ArgIterator(Error) { 190 .iter = ArgIterator(Error){ .nextFn = nextFn },
195 .nextFn = nextFn,
196 },
197 }; 191 };
198 } 192 }
199 193
@@ -204,7 +198,7 @@ pub const OsArgIterator = struct {
204 fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { 198 fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 {
205 const self = @fieldParentPtr(OsArgIterator, "iter", iter); 199 const self = @fieldParentPtr(OsArgIterator, "iter", iter);
206 if (builtin.os == builtin.Os.windows) { 200 if (builtin.os == builtin.Os.windows) {
207 return try self.args.next(self.allocator) ?? return null; 201 return try self.args.next(self.allocator) orelse return null;
208 } else { 202 } else {
209 return self.args.nextPosix(); 203 return self.args.nextPosix();
210 } 204 }
@@ -233,7 +227,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
233 state: State, 227 state: State,
234 228
235 pub fn init(params: []const Param(Id), iter: *ArgIterator(ArgError)) Self { 229 pub fn init(params: []const Param(Id), iter: *ArgIterator(ArgError)) Self {
236 var res = Self { 230 var res = Self{
237 .params = params, 231 .params = params,
238 .iter = iter, 232 .iter = iter,
239 .state = State.Normal, 233 .state = State.Normal,
@@ -245,7 +239,11 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
245 /// Get the next ::Arg that matches a ::Param. 239 /// Get the next ::Arg that matches a ::Param.
246 pub fn next(clap: *Self) !?Arg(Id) { 240 pub fn next(clap: *Self) !?Arg(Id) {
247 const ArgInfo = struct { 241 const ArgInfo = struct {
248 const Kind = enum { Long, Short, Bare }; 242 const Kind = enum {
243 Long,
244 Short,
245 Bare,
246 };
249 247
250 arg: []const u8, 248 arg: []const u8,
251 kind: Kind, 249 kind: Kind,
@@ -253,7 +251,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
253 251
254 switch (clap.state) { 252 switch (clap.state) {
255 State.Normal => { 253 State.Normal => {
256 const full_arg = (try clap.iter.next()) ?? return null; 254 const full_arg = (try clap.iter.next()) orelse return null;
257 const arg_info = blk: { 255 const arg_info = blk: {
258 var arg = full_arg; 256 var arg = full_arg;
259 var kind = ArgInfo.Kind.Bare; 257 var kind = ArgInfo.Kind.Bare;
@@ -271,7 +269,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
271 if (kind != ArgInfo.Kind.Long and arg.len == 0) 269 if (kind != ArgInfo.Kind.Long and arg.len == 0)
272 return error.InvalidArgument; 270 return error.InvalidArgument;
273 271
274 break :blk ArgInfo { .arg = arg, .kind = kind }; 272 break :blk ArgInfo{ .arg = arg, .kind = kind };
275 }; 273 };
276 274
277 const arg = arg_info.arg; 275 const arg = arg_info.arg;
@@ -279,16 +277,15 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
279 const eql_index = mem.indexOfScalar(u8, arg, '='); 277 const eql_index = mem.indexOfScalar(u8, arg, '=');
280 278
281 switch (kind) { 279 switch (kind) {
282 ArgInfo.Kind.Bare, 280 ArgInfo.Kind.Bare, ArgInfo.Kind.Long => {
283 ArgInfo.Kind.Long => {
284 for (clap.params) |*param| { 281 for (clap.params) |*param| {
285 const match = switch (kind) { 282 const match = switch (kind) {
286 ArgInfo.Kind.Bare => param.names.bare ?? continue, 283 ArgInfo.Kind.Bare => param.names.bare orelse continue,
287 ArgInfo.Kind.Long => param.names.long ?? continue, 284 ArgInfo.Kind.Long => param.names.long orelse continue,
288 else => unreachable, 285 else => unreachable,
289 }; 286 };
290 const name = if (eql_index) |i| arg[0..i] else arg; 287 const name = if (eql_index) |i| arg[0..i] else arg;
291 const maybe_value = if (eql_index) |i| arg[i + 1..] else null; 288 const maybe_value = if (eql_index) |i| arg[i + 1 ..] else null;
292 289
293 if (!mem.eql(u8, name, match)) 290 if (!mem.eql(u8, name, match))
294 continue; 291 continue;
@@ -303,14 +300,14 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
303 if (maybe_value) |v| 300 if (maybe_value) |v|
304 break :blk v; 301 break :blk v;
305 302
306 break :blk (try clap.iter.next()) ?? return error.MissingValue; 303 break :blk (try clap.iter.next()) orelse return error.MissingValue;
307 }; 304 };
308 305
309 return Arg(Id).init(param, value); 306 return Arg(Id).init(param, value);
310 } 307 }
311 }, 308 },
312 ArgInfo.Kind.Short => { 309 ArgInfo.Kind.Short => {
313 return try clap.chainging(State.Chaining { 310 return try clap.chainging(State.Chaining{
314 .arg = full_arg, 311 .arg = full_arg,
315 .index = (full_arg.len - arg.len), 312 .index = (full_arg.len - arg.len),
316 }); 313 });
@@ -340,7 +337,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
340 const next_index = index + 1; 337 const next_index = index + 1;
341 338
342 for (clap.params) |*param| { 339 for (clap.params) |*param| {
343 const short = param.names.short ?? continue; 340 const short = param.names.short orelse continue;
344 if (short != arg[index]) 341 if (short != arg[index])
345 continue; 342 continue;
346 343
@@ -349,10 +346,12 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
349 if (arg.len <= next_index or param.takes_value) { 346 if (arg.len <= next_index or param.takes_value) {
350 clap.state = State.Normal; 347 clap.state = State.Normal;
351 } else { 348 } else {
352 clap.state = State { .Chaining = State.Chaining { 349 clap.state = State{
353 .arg = arg, 350 .Chaining = State.Chaining{
354 .index = next_index, 351 .arg = arg,
355 }}; 352 .index = next_index,
353 },
354 };
356 } 355 }
357 } 356 }
358 357
@@ -360,12 +359,12 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type {
360 return Arg(Id).init(param, null); 359 return Arg(Id).init(param, null);
361 360
362 if (arg.len <= next_index) { 361 if (arg.len <= next_index) {
363 const value = (try clap.iter.next()) ?? return error.MissingValue; 362 const value = (try clap.iter.next()) orelse return error.MissingValue;
364 return Arg(Id).init(param, value); 363 return Arg(Id).init(param, value);
365 } 364 }
366 365
367 if (arg[next_index] == '=') { 366 if (arg[next_index] == '=') {
368 return Arg(Id).init(param, arg[next_index + 1..]); 367 return Arg(Id).init(param, arg[next_index + 1 ..]);
369 } 368 }
370 369
371 return Arg(Id).init(param, arg[next_index..]); 370 return Arg(Id).init(param, arg[next_index..]);
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}