Short args can now take values directly after the arg

* closes #5
This commit is contained in:
Jimmi Holst Christensen 2018-04-27 00:25:12 +02:00
parent 5f9278c93e
commit 25e7072686
3 changed files with 23 additions and 10 deletions

View File

@ -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`)

View File

@ -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" {

View File

@ -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 {