diff options
Diffstat (limited to 'core.zig')
| -rw-r--r-- | core.zig | 67 |
1 files changed, 32 insertions, 35 deletions
| @@ -131,10 +131,10 @@ pub const OsArgIterator = struct { | |||
| 131 | }; | 131 | }; |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | fn nextFn(iter: &ArgIterator, allocator: &mem.Allocator) ArgIterator.Error![]const u8 { | 134 | fn nextFn(iter: &ArgIterator, allocator: &mem.Allocator) ArgIterator.Error!?[]const u8 { |
| 135 | const self = @fieldParentPtr(OsArgIterator, "iter", iter); | 135 | const self = @fieldParentPtr(OsArgIterator, "iter", iter); |
| 136 | if (builtin.os == builtin.Os.Windows) { | 136 | if (builtin.os == builtin.Os.windows) { |
| 137 | return try self.args.next(allocator); | 137 | return try self.args.next(allocator) ?? return null; |
| 138 | } else { | 138 | } else { |
| 139 | return self.args.nextPoxix(); | 139 | return self.args.nextPoxix(); |
| 140 | } | 140 | } |
| @@ -161,22 +161,19 @@ pub fn Iterator(comptime Id: type) type { | |||
| 161 | params: []const Param(Id), | 161 | params: []const Param(Id), |
| 162 | inner: &ArgIterator, | 162 | inner: &ArgIterator, |
| 163 | state: State, | 163 | state: State, |
| 164 | command: []const u8, | ||
| 165 | 164 | ||
| 166 | pub fn init(params: []const Param(Id), inner: &ArgIterator, allocator: &mem.Allocator) !Self { | 165 | pub fn init(params: []const Param(Id), inner: &ArgIterator, allocator: &mem.Allocator) Self { |
| 167 | var res = Self { | 166 | var res = Self { |
| 168 | .arena = heap.ArenaAllocator.init(allocator), | 167 | .arena = heap.ArenaAllocator.init(allocator), |
| 169 | .params = params, | 168 | .params = params, |
| 170 | .inner = inner, | 169 | .inner = inner, |
| 171 | .state = State.Normal, | 170 | .state = State.Normal, |
| 172 | .command = undefined, | ||
| 173 | }; | 171 | }; |
| 174 | res.command = (try res.innerNext()) ?? unreachable; | ||
| 175 | 172 | ||
| 176 | return res; | 173 | return res; |
| 177 | } | 174 | } |
| 178 | 175 | ||
| 179 | pub fn deinit(iter: &const Self) void { | 176 | pub fn deinit(iter: &Self) void { |
| 180 | iter.arena.deinit(); | 177 | iter.arena.deinit(); |
| 181 | } | 178 | } |
| 182 | 179 | ||
| @@ -315,7 +312,7 @@ pub fn Iterator(comptime Id: type) type { | |||
| 315 | 312 | ||
| 316 | fn testNoErr(params: []const Param(u8), args: []const []const u8, ids: []const u8, values: []const ?[]const u8) void { | 313 | fn testNoErr(params: []const Param(u8), args: []const []const u8, ids: []const u8, values: []const ?[]const u8) void { |
| 317 | var arg_iter = ArgSliceIterator.init(args); | 314 | var arg_iter = ArgSliceIterator.init(args); |
| 318 | var iter = Iterator(u8).init(params, &arg_iter.iter, debug.global_allocator) catch unreachable; | 315 | var iter = Iterator(u8).init(params, &arg_iter.iter, debug.global_allocator); |
| 319 | 316 | ||
| 320 | var i: usize = 0; | 317 | var i: usize = 0; |
| 321 | while (iter.next() catch unreachable) |arg| : (i += 1) { | 318 | while (iter.next() catch unreachable) |arg| : (i += 1) { |
| @@ -337,15 +334,15 @@ test "clap.parse: short" { | |||
| 337 | Param(u8).init(2, "c", true), | 334 | Param(u8).init(2, "c", true), |
| 338 | }; | 335 | }; |
| 339 | 336 | ||
| 340 | testNoErr(params, [][]const u8 { "command", "-a" }, []u8{0}, []?[]const u8{null}); | 337 | testNoErr(params, [][]const u8 { "-a" }, []u8{0}, []?[]const u8{null}); |
| 341 | testNoErr(params, [][]const u8 { "command", "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null}); | 338 | testNoErr(params, [][]const u8 { "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null}); |
| 342 | testNoErr(params, [][]const u8 { "command", "-ab" }, []u8{0,1}, []?[]const u8{null,null}); | 339 | testNoErr(params, [][]const u8 { "-ab" }, []u8{0,1}, []?[]const u8{null,null}); |
| 343 | testNoErr(params, [][]const u8 { "command", "-c=100" }, []u8{2}, []?[]const u8{"100"}); | 340 | testNoErr(params, [][]const u8 { "-c=100" }, []u8{2}, []?[]const u8{"100"}); |
| 344 | testNoErr(params, [][]const u8 { "command", "-c100" }, []u8{2}, []?[]const u8{"100"}); | 341 | testNoErr(params, [][]const u8 { "-c100" }, []u8{2}, []?[]const u8{"100"}); |
| 345 | testNoErr(params, [][]const u8 { "command", "-c", "100" }, []u8{2}, []?[]const u8{"100"}); | 342 | testNoErr(params, [][]const u8 { "-c", "100" }, []u8{2}, []?[]const u8{"100"}); |
| 346 | testNoErr(params, [][]const u8 { "command", "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 343 | testNoErr(params, [][]const u8 { "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 347 | testNoErr(params, [][]const u8 { "command", "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 344 | testNoErr(params, [][]const u8 { "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 348 | testNoErr(params, [][]const u8 { "command", "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 345 | testNoErr(params, [][]const u8 { "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 349 | } | 346 | } |
| 350 | 347 | ||
| 351 | test "clap.parse: long" { | 348 | test "clap.parse: long" { |
| @@ -355,10 +352,10 @@ test "clap.parse: long" { | |||
| 355 | Param(u8).init(2, "cc", true), | 352 | Param(u8).init(2, "cc", true), |
| 356 | }; | 353 | }; |
| 357 | 354 | ||
| 358 | testNoErr(params, [][]const u8 { "command", "--aa" }, []u8{0}, []?[]const u8{null}); | 355 | testNoErr(params, [][]const u8 { "--aa" }, []u8{0}, []?[]const u8{null}); |
| 359 | testNoErr(params, [][]const u8 { "command", "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null}); | 356 | testNoErr(params, [][]const u8 { "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null}); |
| 360 | testNoErr(params, [][]const u8 { "command", "--cc=100" }, []u8{2}, []?[]const u8{"100"}); | 357 | testNoErr(params, [][]const u8 { "--cc=100" }, []u8{2}, []?[]const u8{"100"}); |
| 361 | testNoErr(params, [][]const u8 { "command", "--cc", "100" }, []u8{2}, []?[]const u8{"100"}); | 358 | testNoErr(params, [][]const u8 { "--cc", "100" }, []u8{2}, []?[]const u8{"100"}); |
| 362 | } | 359 | } |
| 363 | 360 | ||
| 364 | test "clap.parse: both" { | 361 | test "clap.parse: both" { |
| @@ -368,17 +365,17 @@ test "clap.parse: both" { | |||
| 368 | Param(u8).both(2, 'c', "cc", true), | 365 | Param(u8).both(2, 'c', "cc", true), |
| 369 | }; | 366 | }; |
| 370 | 367 | ||
| 371 | testNoErr(params, [][]const u8 { "command", "-a" }, []u8{0}, []?[]const u8{null}); | 368 | testNoErr(params, [][]const u8 { "-a" }, []u8{0}, []?[]const u8{null}); |
| 372 | testNoErr(params, [][]const u8 { "command", "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null}); | 369 | testNoErr(params, [][]const u8 { "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null}); |
| 373 | testNoErr(params, [][]const u8 { "command", "-ab" }, []u8{0,1}, []?[]const u8{null,null}); | 370 | testNoErr(params, [][]const u8 { "-ab" }, []u8{0,1}, []?[]const u8{null,null}); |
| 374 | testNoErr(params, [][]const u8 { "command", "-c=100" }, []u8{2}, []?[]const u8{"100"}); | 371 | testNoErr(params, [][]const u8 { "-c=100" }, []u8{2}, []?[]const u8{"100"}); |
| 375 | testNoErr(params, [][]const u8 { "command", "-c100" }, []u8{2}, []?[]const u8{"100"}); | 372 | testNoErr(params, [][]const u8 { "-c100" }, []u8{2}, []?[]const u8{"100"}); |
| 376 | testNoErr(params, [][]const u8 { "command", "-c", "100" }, []u8{2}, []?[]const u8{"100"}); | 373 | testNoErr(params, [][]const u8 { "-c", "100" }, []u8{2}, []?[]const u8{"100"}); |
| 377 | testNoErr(params, [][]const u8 { "command", "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 374 | testNoErr(params, [][]const u8 { "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 378 | testNoErr(params, [][]const u8 { "command", "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 375 | testNoErr(params, [][]const u8 { "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 379 | testNoErr(params, [][]const u8 { "command", "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); | 376 | testNoErr(params, [][]const u8 { "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"}); |
| 380 | testNoErr(params, [][]const u8 { "command", "--aa" }, []u8{0}, []?[]const u8{null}); | 377 | testNoErr(params, [][]const u8 { "--aa" }, []u8{0}, []?[]const u8{null}); |
| 381 | testNoErr(params, [][]const u8 { "command", "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null}); | 378 | testNoErr(params, [][]const u8 { "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null}); |
| 382 | testNoErr(params, [][]const u8 { "command", "--cc=100" }, []u8{2}, []?[]const u8{"100"}); | 379 | testNoErr(params, [][]const u8 { "--cc=100" }, []u8{2}, []?[]const u8{"100"}); |
| 383 | testNoErr(params, [][]const u8 { "command", "--cc", "100" }, []u8{2}, []?[]const u8{"100"}); | 380 | testNoErr(params, [][]const u8 { "--cc", "100" }, []u8{2}, []?[]const u8{"100"}); |
| 384 | } | 381 | } |