diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/comptime.zig | 19 | ||||
| -rw-r--r-- | src/streaming.zig | 30 |
2 files changed, 27 insertions, 22 deletions
diff --git a/src/comptime.zig b/src/comptime.zig index b585598..d872b82 100644 --- a/src/comptime.zig +++ b/src/comptime.zig | |||
| @@ -4,6 +4,7 @@ const std = @import("std"); | |||
| 4 | const testing = std.testing; | 4 | const testing = std.testing; |
| 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 | 8 | ||
| 8 | pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { | 9 | pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) type { |
| 9 | var flags: usize = 0; | 10 | var flags: usize = 0; |
| @@ -56,11 +57,17 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 56 | if (param.names.long == null and param.names.short == null) { | 57 | if (param.names.long == null and param.names.short == null) { |
| 57 | try pos.append(arg.value.?); | 58 | try pos.append(arg.value.?); |
| 58 | } else if (param.takes_value) { | 59 | } else if (param.takes_value) { |
| 59 | // We slice before access to avoid false positive access out of bound | 60 | // If we don't have any optional parameters, then this code should |
| 60 | // compile error. | 61 | // never be reached. |
| 61 | res.options[0..][param.id] = arg.value.?; | 62 | debug.assert(res.options.len != 0); |
| 63 | |||
| 64 | // Hack: Utilize Zigs lazy analyzis to avoid a compiler error | ||
| 65 | if (res.options.len != 0) | ||
| 66 | res.options[param.id] = arg.value.?; | ||
| 62 | } else { | 67 | } else { |
| 63 | res.flags[0..][param.id] = true; | 68 | debug.assert(res.flags.len != 0); |
| 69 | if (res.flags.len != 0) | ||
| 70 | res.flags[param.id] = true; | ||
| 64 | } | 71 | } |
| 65 | } | 72 | } |
| 66 | 73 | ||
| @@ -118,13 +125,13 @@ test "clap.comptime.ComptimeClap" { | |||
| 118 | .names = clap.Names{ | 125 | .names = clap.Names{ |
| 119 | .short = 'a', | 126 | .short = 'a', |
| 120 | .long = "aa", | 127 | .long = "aa", |
| 121 | } | 128 | }, |
| 122 | }, | 129 | }, |
| 123 | clap.Param(void){ | 130 | clap.Param(void){ |
| 124 | .names = clap.Names{ | 131 | .names = clap.Names{ |
| 125 | .short = 'b', | 132 | .short = 'b', |
| 126 | .long = "bb", | 133 | .long = "bb", |
| 127 | } | 134 | }, |
| 128 | }, | 135 | }, |
| 129 | clap.Param(void){ | 136 | clap.Param(void){ |
| 130 | .names = clap.Names{ | 137 | .names = clap.Names{ |
diff --git a/src/streaming.zig b/src/streaming.zig index 9da120c..d23471c 100644 --- a/src/streaming.zig +++ b/src/streaming.zig | |||
| @@ -174,7 +174,7 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r | |||
| 174 | var iter = args.SliceIterator{ .args = args_strings }; | 174 | var iter = args.SliceIterator{ .args = args_strings }; |
| 175 | var c = StreamingClap(u8, args.SliceIterator){ | 175 | var c = StreamingClap(u8, args.SliceIterator){ |
| 176 | .params = params, | 176 | .params = params, |
| 177 | .iter = &iter | 177 | .iter = &iter, |
| 178 | }; | 178 | }; |
| 179 | 179 | ||
| 180 | for (results) |res| { | 180 | for (results) |res| { |
| @@ -217,9 +217,9 @@ test "clap.streaming.StreamingClap: short params" { | |||
| 217 | testNoErr( | 217 | testNoErr( |
| 218 | params, | 218 | params, |
| 219 | [_][]const u8{ | 219 | [_][]const u8{ |
| 220 | "-a", "-b", "-ab", "-ba", | 220 | "-a", "-b", "-ab", "-ba", |
| 221 | "-c", "0", "-c=0", "-ac", | 221 | "-c", "0", "-c=0", "-ac", |
| 222 | "0", "-ac=0", | 222 | "0", "-ac=0", |
| 223 | }, | 223 | }, |
| 224 | [_]Arg(u8){ | 224 | [_]Arg(u8){ |
| 225 | Arg(u8){ .param = a }, | 225 | Arg(u8){ .param = a }, |
| @@ -262,8 +262,8 @@ test "clap.streaming.StreamingClap: long params" { | |||
| 262 | testNoErr( | 262 | testNoErr( |
| 263 | params, | 263 | params, |
| 264 | [_][]const u8{ | 264 | [_][]const u8{ |
| 265 | "--aa", "--bb", | 265 | "--aa", "--bb", |
| 266 | "--cc", "0", | 266 | "--cc", "0", |
| 267 | "--cc=0", | 267 | "--cc=0", |
| 268 | }, | 268 | }, |
| 269 | [_]Arg(u8){ | 269 | [_]Arg(u8){ |
| @@ -276,12 +276,10 @@ test "clap.streaming.StreamingClap: long params" { | |||
| 276 | } | 276 | } |
| 277 | 277 | ||
| 278 | test "clap.streaming.StreamingClap: positional params" { | 278 | test "clap.streaming.StreamingClap: positional params" { |
| 279 | const params = [_]clap.Param(u8){ | 279 | const params = [_]clap.Param(u8){clap.Param(u8){ |
| 280 | clap.Param(u8){ | 280 | .id = 0, |
| 281 | .id = 0, | 281 | .takes_value = true, |
| 282 | .takes_value = true, | 282 | }}; |
| 283 | }, | ||
| 284 | }; | ||
| 285 | 283 | ||
| 286 | testNoErr( | 284 | testNoErr( |
| 287 | params, | 285 | params, |
| @@ -331,10 +329,10 @@ test "clap.streaming.StreamingClap: all params" { | |||
| 331 | testNoErr( | 329 | testNoErr( |
| 332 | params, | 330 | params, |
| 333 | [_][]const u8{ | 331 | [_][]const u8{ |
| 334 | "-a", "-b", "-ab", "-ba", | 332 | "-a", "-b", "-ab", "-ba", |
| 335 | "-c", "0", "-c=0", "-ac", | 333 | "-c", "0", "-c=0", "-ac", |
| 336 | "0", "-ac=0", "--aa", "--bb", | 334 | "0", "-ac=0", "--aa", "--bb", |
| 337 | "--cc", "0", "--cc=0", "something", | 335 | "--cc", "0", "--cc=0", "something", |
| 338 | }, | 336 | }, |
| 339 | [_]Arg(u8){ | 337 | [_]Arg(u8){ |
| 340 | Arg(u8){ .param = aa }, | 338 | Arg(u8){ .param = aa }, |