diff options
Diffstat (limited to 'src/comptime.zig')
| -rw-r--r-- | src/comptime.zig | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/src/comptime.zig b/src/comptime.zig index b9021de..b585598 100644 --- a/src/comptime.zig +++ b/src/comptime.zig | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | const clap = @import("index.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 testing = std.testing; |
| @@ -8,7 +8,7 @@ const mem = std.mem; | |||
| 8 | pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { | 8 | pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { |
| 9 | var flags: usize = 0; | 9 | var flags: usize = 0; |
| 10 | var options: usize = 0; | 10 | var options: usize = 0; |
| 11 | var converted_params: []const clap.Param(usize) = []clap.Param(usize){}; | 11 | var converted_params: []const clap.Param(usize) = [_]clap.Param(usize){}; |
| 12 | for (params) |param| { | 12 | for (params) |param| { |
| 13 | const index = blk: { | 13 | const index = blk: { |
| 14 | if (param.names.long == null and param.names.short == null) | 14 | if (param.names.long == null and param.names.short == null) |
| @@ -24,8 +24,12 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 24 | break :blk res; | 24 | break :blk res; |
| 25 | }; | 25 | }; |
| 26 | 26 | ||
| 27 | const converted = clap.Param(usize).init(index, param.takes_value, param.names); | 27 | const converted = clap.Param(usize){ |
| 28 | converted_params = converted_params ++ []clap.Param(usize){converted}; | 28 | .id = index, |
| 29 | .names = param.names, | ||
| 30 | .takes_value = param.takes_value, | ||
| 31 | }; | ||
| 32 | converted_params = converted_params ++ [_]clap.Param(usize){converted}; | ||
| 29 | } | 33 | } |
| 30 | 34 | ||
| 31 | return struct { | 35 | return struct { |
| @@ -37,13 +41,16 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 37 | pub fn parse(allocator: *mem.Allocator, comptime ArgIter: type, iter: *ArgIter) !@This() { | 41 | pub fn parse(allocator: *mem.Allocator, comptime ArgIter: type, iter: *ArgIter) !@This() { |
| 38 | var pos = std.ArrayList([]const u8).init(allocator); | 42 | var pos = std.ArrayList([]const u8).init(allocator); |
| 39 | var res = @This(){ | 43 | var res = @This(){ |
| 40 | .options = []?[]const u8{null} ** options, | 44 | .options = [_]?[]const u8{null} ** options, |
| 41 | .flags = []bool{false} ** flags, | 45 | .flags = [_]bool{false} ** flags, |
| 42 | .pos = undefined, | 46 | .pos = undefined, |
| 43 | .allocator = allocator, | 47 | .allocator = allocator, |
| 44 | }; | 48 | }; |
| 45 | 49 | ||
| 46 | var stream = clap.StreamingClap(usize, ArgIter).init(converted_params, iter); | 50 | var stream = clap.StreamingClap(usize, ArgIter){ |
| 51 | .params = converted_params, | ||
| 52 | .iter = iter, | ||
| 53 | }; | ||
| 47 | while (try stream.next()) |arg| { | 54 | while (try stream.next()) |arg| { |
| 48 | const param = arg.param; | 55 | const param = arg.param; |
| 49 | if (param.names.long == null and param.names.short == null) { | 56 | if (param.names.long == null and param.names.short == null) { |
| @@ -90,7 +97,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 90 | comptime { | 97 | comptime { |
| 91 | for (converted_params) |param| { | 98 | for (converted_params) |param| { |
| 92 | if (param.names.short) |s| { | 99 | if (param.names.short) |s| { |
| 93 | if (mem.eql(u8, name, "-" ++ []u8{s})) | 100 | if (mem.eql(u8, name, "-" ++ [_]u8{s})) |
| 94 | return param; | 101 | return param; |
| 95 | } | 102 | } |
| 96 | if (param.names.long) |l| { | 103 | if (param.names.long) |l| { |
| @@ -106,27 +113,38 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 106 | } | 113 | } |
| 107 | 114 | ||
| 108 | test "clap.comptime.ComptimeClap" { | 115 | test "clap.comptime.ComptimeClap" { |
| 109 | const Clap = ComptimeClap(void, comptime []clap.Param(void){ | 116 | const Clap = ComptimeClap(void, [_]clap.Param(void){ |
| 110 | clap.Param(void).flag({}, clap.Names{ | 117 | clap.Param(void){ |
| 111 | .short = 'a', | 118 | .names = clap.Names{ |
| 112 | .long = "aa", | 119 | .short = 'a', |
| 113 | }), | 120 | .long = "aa", |
| 114 | clap.Param(void).flag({}, clap.Names{ | 121 | } |
| 115 | .short = 'b', | 122 | }, |
| 116 | .long = "bb", | 123 | clap.Param(void){ |
| 117 | }), | 124 | .names = clap.Names{ |
| 118 | clap.Param(void).option({}, clap.Names{ | 125 | .short = 'b', |
| 119 | .short = 'c', | 126 | .long = "bb", |
| 120 | .long = "cc", | 127 | } |
| 121 | }), | 128 | }, |
| 122 | clap.Param(void).positional({}), | 129 | clap.Param(void){ |
| 130 | .names = clap.Names{ | ||
| 131 | .short = 'c', | ||
| 132 | .long = "cc", | ||
| 133 | }, | ||
| 134 | .takes_value = true, | ||
| 135 | }, | ||
| 136 | clap.Param(void){ | ||
| 137 | .takes_value = true, | ||
| 138 | }, | ||
| 123 | }); | 139 | }); |
| 124 | 140 | ||
| 125 | var buf: [1024]u8 = undefined; | 141 | var buf: [1024]u8 = undefined; |
| 126 | var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]); | 142 | var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]); |
| 127 | var iter = clap.args.SliceIterator.init([][]const u8{ | 143 | var iter = clap.args.SliceIterator{ |
| 128 | "-a", "-c", "0", "something", | 144 | .args = [_][]const u8{ |
| 129 | }); | 145 | "-a", "-c", "0", "something", |
| 146 | }, | ||
| 147 | }; | ||
| 130 | var args = try Clap.parse(&fb_allocator.allocator, clap.args.SliceIterator, &iter); | 148 | var args = try Clap.parse(&fb_allocator.allocator, clap.args.SliceIterator, &iter); |
| 131 | defer args.deinit(); | 149 | defer args.deinit(); |
| 132 | 150 | ||