diff options
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | clap.zig | 13 | ||||
| -rw-r--r-- | example.zig | 17 |
3 files changed, 23 insertions, 10 deletions
| @@ -13,4 +13,5 @@ See [example](example.zig). | |||
| 13 | * Chaining `-abc` where `a` and `b` does not take values. | 13 | * Chaining `-abc` where `a` and `b` does not take values. |
| 14 | * Long arguments `--long` | 14 | * Long arguments `--long` |
| 15 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) | 15 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 16 | * Both work with chaining (`-ba 100`, `-ba=100`) | 16 | * Short args also support passing values with no spacing or `=` (`-a100`) |
| 17 | * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) | ||
| @@ -162,11 +162,16 @@ pub fn Clap(comptime Result: type) type { | |||
| 162 | 162 | ||
| 163 | if (has_right_index) { | 163 | if (has_right_index) { |
| 164 | if (short_arg == short) { | 164 | if (short_arg == short) { |
| 165 | if (option.takes_value) |_| return error.OptionMissingValue; | 165 | if (option.takes_value) |parser| { |
| 166 | const value = arg[i + 1..]; | ||
| 167 | try parser.parse(&@field(result, option.field), value); | ||
| 168 | break :success; | ||
| 169 | } else { | ||
| 170 | @field(result, option.field) = true; | ||
| 171 | continue :short_arg_loop; | ||
| 172 | } | ||
| 166 | 173 | ||
| 167 | @field(result, option.field) = true; | ||
| 168 | required = newRequired(option, required, required_index); | 174 | required = newRequired(option, required, required_index); |
| 169 | continue :short_arg_loop; | ||
| 170 | } | 175 | } |
| 171 | } | 176 | } |
| 172 | } | 177 | } |
| @@ -420,10 +425,12 @@ test "clap.parse: short" { | |||
| 420 | testNoErr(clap, [][]const u8 { "-a" }, default.with("a", true)); | 425 | testNoErr(clap, [][]const u8 { "-a" }, default.with("a", true)); |
| 421 | testNoErr(clap, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true)); | 426 | testNoErr(clap, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true)); |
| 422 | testNoErr(clap, [][]const u8 { "-i=100" }, default.with("int", 100)); | 427 | testNoErr(clap, [][]const u8 { "-i=100" }, default.with("int", 100)); |
| 428 | testNoErr(clap, [][]const u8 { "-i100" }, default.with("int", 100)); | ||
| 423 | testNoErr(clap, [][]const u8 { "-i", "100" }, default.with("int", 100)); | 429 | testNoErr(clap, [][]const u8 { "-i", "100" }, default.with("int", 100)); |
| 424 | testNoErr(clap, [][]const u8 { "-ab" }, default.with("a", true).with("b", true)); | 430 | testNoErr(clap, [][]const u8 { "-ab" }, default.with("a", true).with("b", true)); |
| 425 | testNoErr(clap, [][]const u8 { "-abi", "100" }, default.with("a", true).with("b", true).with("int", 100)); | 431 | testNoErr(clap, [][]const u8 { "-abi", "100" }, default.with("a", true).with("b", true).with("int", 100)); |
| 426 | testNoErr(clap, [][]const u8 { "-abi=100" }, default.with("a", true).with("b", true).with("int", 100)); | 432 | testNoErr(clap, [][]const u8 { "-abi=100" }, default.with("a", true).with("b", true).with("int", 100)); |
| 433 | testNoErr(clap, [][]const u8 { "-abi100" }, default.with("a", true).with("b", true).with("int", 100)); | ||
| 427 | } | 434 | } |
| 428 | 435 | ||
| 429 | test "clap.parse: long" { | 436 | test "clap.parse: long" { |
diff --git a/example.zig b/example.zig index c77ef5c..4d63b02 100644 --- a/example.zig +++ b/example.zig | |||
| @@ -22,12 +22,17 @@ const Options = struct { | |||
| 22 | // a = 1 | 22 | // a = 1 |
| 23 | // zig-clap> .\example.exe -pa 1 | 23 | // zig-clap> .\example.exe -pa 1 |
| 24 | // a = 1 | 24 | // a = 1 |
| 25 | // zig-clap> .\example.exe -pd friend | 25 | // zig-clap> .\example.exe -pd V1 |
| 26 | // d = friend | 26 | // d = V1 |
| 27 | // zig-clap> .\example.exe -pd=friend | 27 | // zig-clap> .\example.exe -pd=V2 |
| 28 | // d = friend | 28 | // d = V2 |
| 29 | // zig-clap> .\example.exe -p -d=friend | 29 | // zig-clap> .\example.exe -p -d=V3 |
| 30 | // d = friend | 30 | // d = V3 |
| 31 | // zig-clap> .\example.exe -pdV=4 | ||
| 32 | // d = V=4 | ||
| 33 | // zig-clap> .\example.exe -p -dV=5 | ||
| 34 | // d = V=5 | ||
| 35 | |||
| 31 | pub fn main() !void { | 36 | pub fn main() !void { |
| 32 | const parser = comptime Clap(Options).init( | 37 | const parser = comptime Clap(Options).init( |
| 33 | Options { | 38 | Options { |