diff options
Diffstat (limited to 'clap/comptime.zig')
| -rw-r--r-- | clap/comptime.zig | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig index 8ab61cb..9bec38e 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | const clap = @import("../clap.zig"); | 1 | const clap = @import("../clap.zig"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | const testing = std.testing; | 4 | const debug = std.debug; |
| 5 | const heap = std.heap; | 5 | const heap = std.heap; |
| 6 | const mem = std.mem; | 6 | const mem = std.mem; |
| 7 | const debug = std.debug; | 7 | const testing = std.testing; |
| 8 | 8 | ||
| 9 | /// Deprecated: Use `parseEx` instead | 9 | /// Deprecated: Use `parseEx` instead |
| 10 | pub fn ComptimeClap( | 10 | pub fn ComptimeClap( |
| @@ -42,7 +42,8 @@ pub fn ComptimeClap( | |||
| 42 | pos: []const []const u8, | 42 | pos: []const []const u8, |
| 43 | allocator: *mem.Allocator, | 43 | allocator: *mem.Allocator, |
| 44 | 44 | ||
| 45 | pub fn parse(allocator: *mem.Allocator, iter: anytype, diag: ?*clap.Diagnostic) !@This() { | 45 | pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() { |
| 46 | const allocator = opt.allocator; | ||
| 46 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; | 47 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; |
| 47 | for (multis) |*multi| { | 48 | for (multis) |*multi| { |
| 48 | multi.* = std.ArrayList([]const u8).init(allocator); | 49 | multi.* = std.ArrayList([]const u8).init(allocator); |
| @@ -62,7 +63,7 @@ pub fn ComptimeClap( | |||
| 62 | .params = converted_params, | 63 | .params = converted_params, |
| 63 | .iter = iter, | 64 | .iter = iter, |
| 64 | }; | 65 | }; |
| 65 | while (try stream.next(diag)) |arg| { | 66 | while (try stream.next()) |arg| { |
| 66 | const param = arg.param; | 67 | const param = arg.param; |
| 67 | if (param.names.long == null and param.names.short == null) { | 68 | if (param.names.long == null and param.names.short == null) { |
| 68 | try pos.append(arg.value.?); | 69 | try pos.append(arg.value.?); |
| @@ -81,19 +82,17 @@ pub fn ComptimeClap( | |||
| 81 | } | 82 | } |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | for (multis) |*multi, i| { | 85 | for (multis) |*multi, i| |
| 85 | res.multi_options[i] = multi.toOwnedSlice(); | 86 | res.multi_options[i] = multi.toOwnedSlice(); |
| 86 | } | ||
| 87 | res.pos = pos.toOwnedSlice(); | 87 | res.pos = pos.toOwnedSlice(); |
| 88 | 88 | ||
| 89 | return res; | 89 | return res; |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | pub fn deinit(parser: *@This()) void { | 92 | pub fn deinit(parser: @This()) void { |
| 93 | for (parser.multi_options) |o| | 93 | for (parser.multi_options) |o| |
| 94 | parser.allocator.free(o); | 94 | parser.allocator.free(o); |
| 95 | parser.allocator.free(parser.pos); | 95 | parser.allocator.free(parser.pos); |
| 96 | parser.* = undefined; | ||
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | pub fn flag(parser: @This(), comptime name: []const u8) bool { | 98 | pub fn flag(parser: @This(), comptime name: []const u8) bool { |
| @@ -155,14 +154,12 @@ test "" { | |||
| 155 | clap.parseParam("<P>") catch unreachable, | 154 | clap.parseParam("<P>") catch unreachable, |
| 156 | }); | 155 | }); |
| 157 | 156 | ||
| 158 | var buf: [1024]u8 = undefined; | ||
| 159 | var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]); | ||
| 160 | var iter = clap.args.SliceIterator{ | 157 | var iter = clap.args.SliceIterator{ |
| 161 | .args = &[_][]const u8{ | 158 | .args = &[_][]const u8{ |
| 162 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", | 159 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", |
| 163 | }, | 160 | }, |
| 164 | }; | 161 | }; |
| 165 | var args = try Clap.parse(&fb_allocator.allocator, &iter, null); | 162 | var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); |
| 166 | defer args.deinit(); | 163 | defer args.deinit(); |
| 167 | 164 | ||
| 168 | testing.expect(args.flag("-a")); | 165 | testing.expect(args.flag("-a")); |