summaryrefslogtreecommitdiff
path: root/src/core.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core.zig65
1 files changed, 32 insertions, 33 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..]);