diff options
Diffstat (limited to 'src/streaming.zig')
| -rw-r--r-- | src/streaming.zig | 217 |
1 files changed, 120 insertions, 97 deletions
diff --git a/src/streaming.zig b/src/streaming.zig index 7bdc71f..9da120c 100644 --- a/src/streaming.zig +++ b/src/streaming.zig | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | const builtin = @import("builtin"); | 1 | const builtin = @import("builtin"); |
| 2 | const clap = @import("index.zig"); | 2 | const clap = @import("../clap.zig"); |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| 4 | 4 | ||
| 5 | const args = clap.args; | 5 | const args = clap.args; |
| @@ -14,14 +14,7 @@ pub fn Arg(comptime Id: type) type { | |||
| 14 | const Self = @This(); | 14 | const Self = @This(); |
| 15 | 15 | ||
| 16 | param: *const clap.Param(Id), | 16 | param: *const clap.Param(Id), |
| 17 | value: ?[]const u8, | 17 | value: ?[]const u8 = null, |
| 18 | |||
| 19 | pub fn init(param: *const clap.Param(Id), value: ?[]const u8) Self { | ||
| 20 | return Self{ | ||
| 21 | .param = param, | ||
| 22 | .value = value, | ||
| 23 | }; | ||
| 24 | } | ||
| 25 | }; | 18 | }; |
| 26 | } | 19 | } |
| 27 | 20 | ||
| @@ -42,17 +35,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 42 | 35 | ||
| 43 | params: []const clap.Param(Id), | 36 | params: []const clap.Param(Id), |
| 44 | iter: *ArgIterator, | 37 | iter: *ArgIterator, |
| 45 | state: State, | 38 | state: State = State.Normal, |
| 46 | |||
| 47 | pub fn init(params: []const clap.Param(Id), iter: *ArgIterator) @This() { | ||
| 48 | var res = @This(){ | ||
| 49 | .params = params, | ||
| 50 | .iter = iter, | ||
| 51 | .state = State.Normal, | ||
| 52 | }; | ||
| 53 | |||
| 54 | return res; | ||
| 55 | } | ||
| 56 | 39 | ||
| 57 | /// Get the next ::Arg that matches a ::Param. | 40 | /// Get the next ::Arg that matches a ::Param. |
| 58 | pub fn next(parser: *@This()) !?Arg(Id) { | 41 | pub fn next(parser: *@This()) !?Arg(Id) { |
| @@ -107,7 +90,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 107 | if (maybe_value != null) | 90 | if (maybe_value != null) |
| 108 | return error.DoesntTakeValue; | 91 | return error.DoesntTakeValue; |
| 109 | 92 | ||
| 110 | return Arg(Id).init(param, null); | 93 | return Arg(Id){ .param = param }; |
| 111 | } | 94 | } |
| 112 | 95 | ||
| 113 | const value = blk: { | 96 | const value = blk: { |
| @@ -117,7 +100,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 117 | break :blk (try parser.iter.next()) orelse return error.MissingValue; | 100 | break :blk (try parser.iter.next()) orelse return error.MissingValue; |
| 118 | }; | 101 | }; |
| 119 | 102 | ||
| 120 | return Arg(Id).init(param, value); | 103 | return Arg(Id){ .param = param, .value = value }; |
| 121 | } | 104 | } |
| 122 | }, | 105 | }, |
| 123 | ArgInfo.Kind.Short => { | 106 | ArgInfo.Kind.Short => { |
| @@ -133,7 +116,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 133 | if (param.names.short) |_| | 116 | if (param.names.short) |_| |
| 134 | continue; | 117 | continue; |
| 135 | 118 | ||
| 136 | return Arg(Id).init(param, arg); | 119 | return Arg(Id){ .param = param, .value = arg }; |
| 137 | } | 120 | } |
| 138 | }, | 121 | }, |
| 139 | } | 122 | } |
| @@ -169,18 +152,17 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 169 | } | 152 | } |
| 170 | 153 | ||
| 171 | if (!param.takes_value) | 154 | if (!param.takes_value) |
| 172 | return Arg(Id).init(param, null); | 155 | return Arg(Id){ .param = param }; |
| 173 | 156 | ||
| 174 | if (arg.len <= next_index) { | 157 | if (arg.len <= next_index) { |
| 175 | const value = (try parser.iter.next()) orelse return error.MissingValue; | 158 | const value = (try parser.iter.next()) orelse return error.MissingValue; |
| 176 | return Arg(Id).init(param, value); | 159 | return Arg(Id){ .param = param, .value = value }; |
| 177 | } | 160 | } |
| 178 | 161 | ||
| 179 | if (arg[next_index] == '=') { | 162 | if (arg[next_index] == '=') |
| 180 | return Arg(Id).init(param, arg[next_index + 1 ..]); | 163 | return Arg(Id){ .param = param, .value = arg[next_index + 1 ..] }; |
| 181 | } | ||
| 182 | 164 | ||
| 183 | return Arg(Id).init(param, arg[next_index..]); | 165 | return Arg(Id){ .param = param, .value = arg[next_index..] }; |
| 184 | } | 166 | } |
| 185 | 167 | ||
| 186 | return error.InvalidArgument; | 168 | return error.InvalidArgument; |
| @@ -189,8 +171,11 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 189 | } | 171 | } |
| 190 | 172 | ||
| 191 | fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) void { | 173 | fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, results: []const Arg(u8)) void { |
| 192 | var iter = args.SliceIterator.init(args_strings); | 174 | var iter = args.SliceIterator{ .args = args_strings }; |
| 193 | var c = StreamingClap(u8, args.SliceIterator).init(params, &iter); | 175 | var c = StreamingClap(u8, args.SliceIterator){ |
| 176 | .params = params, | ||
| 177 | .iter = &iter | ||
| 178 | }; | ||
| 194 | 179 | ||
| 195 | for (results) |res| { | 180 | for (results) |res| { |
| 196 | const arg = (c.next() catch unreachable) orelse unreachable; | 181 | const arg = (c.next() catch unreachable) orelse unreachable; |
| @@ -209,10 +194,20 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r | |||
| 209 | } | 194 | } |
| 210 | 195 | ||
| 211 | test "clap.streaming.StreamingClap: short params" { | 196 | test "clap.streaming.StreamingClap: short params" { |
| 212 | const params = []clap.Param(u8){ | 197 | const params = [_]clap.Param(u8){ |
| 213 | clap.Param(u8).flag(0, clap.Names.short('a')), | 198 | clap.Param(u8){ |
| 214 | clap.Param(u8).flag(1, clap.Names.short('b')), | 199 | .id = 0, |
| 215 | clap.Param(u8).option(2, clap.Names.short('c')), | 200 | .names = clap.Names{ .short = 'a' }, |
| 201 | }, | ||
| 202 | clap.Param(u8){ | ||
| 203 | .id = 1, | ||
| 204 | .names = clap.Names{ .short = 'b' }, | ||
| 205 | }, | ||
| 206 | clap.Param(u8){ | ||
| 207 | .id = 2, | ||
| 208 | .names = clap.Names{ .short = 'c' }, | ||
| 209 | .takes_value = true, | ||
| 210 | }, | ||
| 216 | }; | 211 | }; |
| 217 | 212 | ||
| 218 | const a = ¶ms[0]; | 213 | const a = ¶ms[0]; |
| @@ -221,33 +216,43 @@ test "clap.streaming.StreamingClap: short params" { | |||
| 221 | 216 | ||
| 222 | testNoErr( | 217 | testNoErr( |
| 223 | params, | 218 | params, |
| 224 | [][]const u8{ | 219 | [_][]const u8{ |
| 225 | "-a", "-b", "-ab", "-ba", | 220 | "-a", "-b", "-ab", "-ba", |
| 226 | "-c", "0", "-c=0", "-ac", | 221 | "-c", "0", "-c=0", "-ac", |
| 227 | "0", "-ac=0", | 222 | "0", "-ac=0", |
| 228 | }, | 223 | }, |
| 229 | []const Arg(u8){ | 224 | [_]Arg(u8){ |
| 230 | Arg(u8).init(a, null), | 225 | Arg(u8){ .param = a }, |
| 231 | Arg(u8).init(b, null), | 226 | Arg(u8){ .param = b }, |
| 232 | Arg(u8).init(a, null), | 227 | Arg(u8){ .param = a }, |
| 233 | Arg(u8).init(b, null), | 228 | Arg(u8){ .param = b }, |
| 234 | Arg(u8).init(b, null), | 229 | Arg(u8){ .param = b }, |
| 235 | Arg(u8).init(a, null), | 230 | Arg(u8){ .param = a }, |
| 236 | Arg(u8).init(c, "0"), | 231 | Arg(u8){ .param = c, .value = "0" }, |
| 237 | Arg(u8).init(c, "0"), | 232 | Arg(u8){ .param = c, .value = "0" }, |
| 238 | Arg(u8).init(a, null), | 233 | Arg(u8){ .param = a }, |
| 239 | Arg(u8).init(c, "0"), | 234 | Arg(u8){ .param = c, .value = "0" }, |
| 240 | Arg(u8).init(a, null), | 235 | Arg(u8){ .param = a }, |
| 241 | Arg(u8).init(c, "0"), | 236 | Arg(u8){ .param = c, .value = "0" }, |
| 242 | }, | 237 | }, |
| 243 | ); | 238 | ); |
| 244 | } | 239 | } |
| 245 | 240 | ||
| 246 | test "clap.streaming.StreamingClap: long params" { | 241 | test "clap.streaming.StreamingClap: long params" { |
| 247 | const params = []clap.Param(u8){ | 242 | const params = [_]clap.Param(u8){ |
| 248 | clap.Param(u8).flag(0, clap.Names.long("aa")), | 243 | clap.Param(u8){ |
| 249 | clap.Param(u8).flag(1, clap.Names.long("bb")), | 244 | .id = 0, |
| 250 | clap.Param(u8).option(2, clap.Names.long("cc")), | 245 | .names = clap.Names{ .long = "aa" }, |
| 246 | }, | ||
| 247 | clap.Param(u8){ | ||
| 248 | .id = 1, | ||
| 249 | .names = clap.Names{ .long = "bb" }, | ||
| 250 | }, | ||
| 251 | clap.Param(u8){ | ||
| 252 | .id = 2, | ||
| 253 | .names = clap.Names{ .long = "cc" }, | ||
| 254 | .takes_value = true, | ||
| 255 | }, | ||
| 251 | }; | 256 | }; |
| 252 | 257 | ||
| 253 | const aa = ¶ms[0]; | 258 | const aa = ¶ms[0]; |
| @@ -256,48 +261,66 @@ test "clap.streaming.StreamingClap: long params" { | |||
| 256 | 261 | ||
| 257 | testNoErr( | 262 | testNoErr( |
| 258 | params, | 263 | params, |
| 259 | [][]const u8{ | 264 | [_][]const u8{ |
| 260 | "--aa", "--bb", | 265 | "--aa", "--bb", |
| 261 | "--cc", "0", | 266 | "--cc", "0", |
| 262 | "--cc=0", | 267 | "--cc=0", |
| 263 | }, | 268 | }, |
| 264 | []const Arg(u8){ | 269 | [_]Arg(u8){ |
| 265 | Arg(u8).init(aa, null), | 270 | Arg(u8){ .param = aa }, |
| 266 | Arg(u8).init(bb, null), | 271 | Arg(u8){ .param = bb }, |
| 267 | Arg(u8).init(cc, "0"), | 272 | Arg(u8){ .param = cc, .value = "0" }, |
| 268 | Arg(u8).init(cc, "0"), | 273 | Arg(u8){ .param = cc, .value = "0" }, |
| 269 | }, | 274 | }, |
| 270 | ); | 275 | ); |
| 271 | } | 276 | } |
| 272 | 277 | ||
| 273 | test "clap.streaming.StreamingClap: positional params" { | 278 | test "clap.streaming.StreamingClap: positional params" { |
| 274 | const params = []clap.Param(u8){clap.Param(u8).positional(0)}; | 279 | const params = [_]clap.Param(u8){ |
| 280 | clap.Param(u8){ | ||
| 281 | .id = 0, | ||
| 282 | .takes_value = true, | ||
| 283 | }, | ||
| 284 | }; | ||
| 275 | 285 | ||
| 276 | testNoErr( | 286 | testNoErr( |
| 277 | params, | 287 | params, |
| 278 | [][]const u8{ "aa", "bb" }, | 288 | [_][]const u8{ "aa", "bb" }, |
| 279 | []const Arg(u8){ | 289 | [_]Arg(u8){ |
| 280 | Arg(u8).init(¶ms[0], "aa"), | 290 | Arg(u8){ .param = ¶ms[0], .value = "aa" }, |
| 281 | Arg(u8).init(¶ms[0], "bb"), | 291 | Arg(u8){ .param = ¶ms[0], .value = "bb" }, |
| 282 | }, | 292 | }, |
| 283 | ); | 293 | ); |
| 284 | } | 294 | } |
| 285 | 295 | ||
| 286 | test "clap.streaming.StreamingClap: all params" { | 296 | test "clap.streaming.StreamingClap: all params" { |
| 287 | const params = []clap.Param(u8){ | 297 | const params = [_]clap.Param(u8){ |
| 288 | clap.Param(u8).flag(0, clap.Names{ | 298 | clap.Param(u8){ |
| 289 | .short = 'a', | 299 | .id = 0, |
| 290 | .long = "aa", | 300 | .names = clap.Names{ |
| 291 | }), | 301 | .short = 'a', |
| 292 | clap.Param(u8).flag(1, clap.Names{ | 302 | .long = "aa", |
| 293 | .short = 'b', | 303 | }, |
| 294 | .long = "bb", | 304 | }, |
| 295 | }), | 305 | clap.Param(u8){ |
| 296 | clap.Param(u8).option(2, clap.Names{ | 306 | .id = 1, |
| 297 | .short = 'c', | 307 | .names = clap.Names{ |
| 298 | .long = "cc", | 308 | .short = 'b', |
| 299 | }), | 309 | .long = "bb", |
| 300 | clap.Param(u8).positional(3), | 310 | }, |
| 311 | }, | ||
| 312 | clap.Param(u8){ | ||
| 313 | .id = 2, | ||
| 314 | .names = clap.Names{ | ||
| 315 | .short = 'c', | ||
| 316 | .long = "cc", | ||
| 317 | }, | ||
| 318 | .takes_value = true, | ||
| 319 | }, | ||
| 320 | clap.Param(u8){ | ||
| 321 | .id = 3, | ||
| 322 | .takes_value = true, | ||
| 323 | }, | ||
| 301 | }; | 324 | }; |
| 302 | 325 | ||
| 303 | const aa = ¶ms[0]; | 326 | const aa = ¶ms[0]; |
| @@ -307,30 +330,30 @@ test "clap.streaming.StreamingClap: all params" { | |||
| 307 | 330 | ||
| 308 | testNoErr( | 331 | testNoErr( |
| 309 | params, | 332 | params, |
| 310 | [][]const u8{ | 333 | [_][]const u8{ |
| 311 | "-a", "-b", "-ab", "-ba", | 334 | "-a", "-b", "-ab", "-ba", |
| 312 | "-c", "0", "-c=0", "-ac", | 335 | "-c", "0", "-c=0", "-ac", |
| 313 | "0", "-ac=0", "--aa", "--bb", | 336 | "0", "-ac=0", "--aa", "--bb", |
| 314 | "--cc", "0", "--cc=0", "something", | 337 | "--cc", "0", "--cc=0", "something", |
| 315 | }, | 338 | }, |
| 316 | []const Arg(u8){ | 339 | [_]Arg(u8){ |
| 317 | Arg(u8).init(aa, null), | 340 | Arg(u8){ .param = aa }, |
| 318 | Arg(u8).init(bb, null), | 341 | Arg(u8){ .param = bb }, |
| 319 | Arg(u8).init(aa, null), | 342 | Arg(u8){ .param = aa }, |
| 320 | Arg(u8).init(bb, null), | 343 | Arg(u8){ .param = bb }, |
| 321 | Arg(u8).init(bb, null), | 344 | Arg(u8){ .param = bb }, |
| 322 | Arg(u8).init(aa, null), | 345 | Arg(u8){ .param = aa }, |
| 323 | Arg(u8).init(cc, "0"), | 346 | Arg(u8){ .param = cc, .value = "0" }, |
| 324 | Arg(u8).init(cc, "0"), | 347 | Arg(u8){ .param = cc, .value = "0" }, |
| 325 | Arg(u8).init(aa, null), | 348 | Arg(u8){ .param = aa }, |
| 326 | Arg(u8).init(cc, "0"), | 349 | Arg(u8){ .param = cc, .value = "0" }, |
| 327 | Arg(u8).init(aa, null), | 350 | Arg(u8){ .param = aa }, |
| 328 | Arg(u8).init(cc, "0"), | 351 | Arg(u8){ .param = cc, .value = "0" }, |
| 329 | Arg(u8).init(aa, null), | 352 | Arg(u8){ .param = aa }, |
| 330 | Arg(u8).init(bb, null), | 353 | Arg(u8){ .param = bb }, |
| 331 | Arg(u8).init(cc, "0"), | 354 | Arg(u8){ .param = cc, .value = "0" }, |
| 332 | Arg(u8).init(cc, "0"), | 355 | Arg(u8){ .param = cc, .value = "0" }, |
| 333 | Arg(u8).init(positional, "something"), | 356 | Arg(u8){ .param = positional, .value = "something" }, |
| 334 | }, | 357 | }, |
| 335 | ); | 358 | ); |
| 336 | } | 359 | } |