diff options
| author | 2019-09-05 23:41:32 +0200 | |
|---|---|---|
| committer | 2019-09-05 23:41:32 +0200 | |
| commit | b23cca0a768d0f4273d1669c2d6a3aa092b35b61 (patch) | |
| tree | 435315750c69840fd659b1ba2925362ae0ea250e /src | |
| parent | correct comptime-clap-error.zig example (diff) | |
| download | zig-clap-b23cca0a768d0f4273d1669c2d6a3aa092b35b61.tar.gz zig-clap-b23cca0a768d0f4273d1669c2d6a3aa092b35b61.tar.xz zig-clap-b23cca0a768d0f4273d1669c2d6a3aa092b35b61.zip | |
always interpret '-' and '--' as positionals
Diffstat (limited to 'src')
| -rw-r--r-- | src/streaming.zig | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/src/streaming.zig b/src/streaming.zig index d23471c..884a270 100644 --- a/src/streaming.zig +++ b/src/streaming.zig | |||
| @@ -53,25 +53,14 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 53 | switch (parser.state) { | 53 | switch (parser.state) { |
| 54 | State.Normal => { | 54 | State.Normal => { |
| 55 | const full_arg = (try parser.iter.next()) orelse return null; | 55 | const full_arg = (try parser.iter.next()) orelse return null; |
| 56 | const arg_info = blk: { | 56 | const arg_info = if (mem.eql(u8, full_arg, "--") or mem.eql(u8, full_arg, "-")) |
| 57 | var arg = full_arg; | 57 | ArgInfo{ .arg = full_arg, .kind = .Positional } |
| 58 | var kind = ArgInfo.Kind.Positional; | 58 | else if (mem.startsWith(u8, full_arg, "--")) |
| 59 | 59 | ArgInfo{ .arg = full_arg[2..], .kind = .Long } | |
| 60 | if (mem.startsWith(u8, arg, "--")) { | 60 | else if (mem.startsWith(u8, full_arg, "-")) |
| 61 | arg = arg[2..]; | 61 | ArgInfo{ .arg = full_arg[1..], .kind = .Short } |
| 62 | kind = ArgInfo.Kind.Long; | 62 | else |
| 63 | } else if (mem.startsWith(u8, arg, "-")) { | 63 | ArgInfo{ .arg = full_arg, .kind = .Positional }; |
| 64 | arg = arg[1..]; | ||
| 65 | kind = ArgInfo.Kind.Short; | ||
| 66 | } | ||
| 67 | |||
| 68 | // We allow long arguments to go without a name. | ||
| 69 | // This allows the user to use "--" for something important | ||
| 70 | if (kind != ArgInfo.Kind.Long and arg.len == 0) | ||
| 71 | return error.InvalidArgument; | ||
| 72 | |||
| 73 | break :blk ArgInfo{ .arg = arg, .kind = kind }; | ||
| 74 | }; | ||
| 75 | 64 | ||
| 76 | const arg = arg_info.arg; | 65 | const arg = arg_info.arg; |
| 77 | const kind = arg_info.kind; | 66 | const kind = arg_info.kind; |
| @@ -333,6 +322,7 @@ test "clap.streaming.StreamingClap: all params" { | |||
| 333 | "-c", "0", "-c=0", "-ac", | 322 | "-c", "0", "-c=0", "-ac", |
| 334 | "0", "-ac=0", "--aa", "--bb", | 323 | "0", "-ac=0", "--aa", "--bb", |
| 335 | "--cc", "0", "--cc=0", "something", | 324 | "--cc", "0", "--cc=0", "something", |
| 325 | "--", "-", | ||
| 336 | }, | 326 | }, |
| 337 | [_]Arg(u8){ | 327 | [_]Arg(u8){ |
| 338 | Arg(u8){ .param = aa }, | 328 | Arg(u8){ .param = aa }, |
| @@ -352,6 +342,8 @@ test "clap.streaming.StreamingClap: all params" { | |||
| 352 | Arg(u8){ .param = cc, .value = "0" }, | 342 | Arg(u8){ .param = cc, .value = "0" }, |
| 353 | Arg(u8){ .param = cc, .value = "0" }, | 343 | Arg(u8){ .param = cc, .value = "0" }, |
| 354 | Arg(u8){ .param = positional, .value = "something" }, | 344 | Arg(u8){ .param = positional, .value = "something" }, |
| 345 | Arg(u8){ .param = positional, .value = "--" }, | ||
| 346 | Arg(u8){ .param = positional, .value = "-" }, | ||
| 355 | }, | 347 | }, |
| 356 | ); | 348 | ); |
| 357 | } | 349 | } |