From 25e7072686cfa3b91df689bf5b76c6fb728003f6 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 27 Apr 2018 00:25:12 +0200 Subject: Short args can now take values directly after the arg * closes #5 --- README.md | 3 ++- clap.zig | 13 ++++++++++--- example.zig | 17 +++++++++++------ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b90f5cd..b4ecc65 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,5 @@ See [example](example.zig). * Chaining `-abc` where `a` and `b` does not take values. * Long arguments `--long` * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) - * Both work with chaining (`-ba 100`, `-ba=100`) + * Short args also support passing values with no spacing or `=` (`-a100`) + * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) diff --git a/clap.zig b/clap.zig index 3cd624e..269e9f1 100644 --- a/clap.zig +++ b/clap.zig @@ -162,11 +162,16 @@ pub fn Clap(comptime Result: type) type { if (has_right_index) { if (short_arg == short) { - if (option.takes_value) |_| return error.OptionMissingValue; + if (option.takes_value) |parser| { + const value = arg[i + 1..]; + try parser.parse(&@field(result, option.field), value); + break :success; + } else { + @field(result, option.field) = true; + continue :short_arg_loop; + } - @field(result, option.field) = true; required = newRequired(option, required, required_index); - continue :short_arg_loop; } } } @@ -420,10 +425,12 @@ test "clap.parse: short" { testNoErr(clap, [][]const u8 { "-a" }, default.with("a", true)); testNoErr(clap, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true)); testNoErr(clap, [][]const u8 { "-i=100" }, default.with("int", 100)); + testNoErr(clap, [][]const u8 { "-i100" }, default.with("int", 100)); testNoErr(clap, [][]const u8 { "-i", "100" }, default.with("int", 100)); testNoErr(clap, [][]const u8 { "-ab" }, default.with("a", true).with("b", true)); testNoErr(clap, [][]const u8 { "-abi", "100" }, default.with("a", true).with("b", true).with("int", 100)); testNoErr(clap, [][]const u8 { "-abi=100" }, default.with("a", true).with("b", true).with("int", 100)); + testNoErr(clap, [][]const u8 { "-abi100" }, default.with("a", true).with("b", true).with("int", 100)); } 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 { // a = 1 // zig-clap> .\example.exe -pa 1 // a = 1 -// zig-clap> .\example.exe -pd friend -// d = friend -// zig-clap> .\example.exe -pd=friend -// d = friend -// zig-clap> .\example.exe -p -d=friend -// d = friend +// zig-clap> .\example.exe -pd V1 +// d = V1 +// zig-clap> .\example.exe -pd=V2 +// d = V2 +// zig-clap> .\example.exe -p -d=V3 +// d = V3 +// zig-clap> .\example.exe -pdV=4 +// d = V=4 +// zig-clap> .\example.exe -p -dV=5 +// d = V=5 + pub fn main() !void { const parser = comptime Clap(Options).init( Options { -- cgit v1.2.3