diff options
| author | 2020-08-23 13:48:12 +1000 | |
|---|---|---|
| committer | 2020-08-28 09:43:42 +0200 | |
| commit | 08bab91e1e8ced3f06c128a57bd991b9a1b901fc (patch) | |
| tree | 0a86df5776f0840b3262697af3c8ed7b1974fb71 /clap/comptime.zig | |
| parent | parse multiple options (diff) | |
| download | zig-clap-08bab91e1e8ced3f06c128a57bd991b9a1b901fc.tar.gz zig-clap-08bab91e1e8ced3f06c128a57bd991b9a1b901fc.tar.xz zig-clap-08bab91e1e8ced3f06c128a57bd991b9a1b901fc.zip | |
parse and validate multiple option
Diffstat (limited to 'clap/comptime.zig')
| -rw-r--r-- | clap/comptime.zig | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig index 90d34e9..cecfcb2 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -13,7 +13,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 13 | for (params) |param| { | 13 | for (params) |param| { |
| 14 | var index: usize = 0; | 14 | var index: usize = 0; |
| 15 | if (param.names.long != null or param.names.short != null) { | 15 | if (param.names.long != null or param.names.short != null) { |
| 16 | const ptr = if (param.takes_value) &options else &flags; | 16 | const ptr = if (param.takes_value != .None) &options else &flags; |
| 17 | index = ptr.*; | 17 | index = ptr.*; |
| 18 | ptr.* += 1; | 18 | ptr.* += 1; |
| 19 | } | 19 | } |
| @@ -52,7 +52,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 52 | const param = arg.param; | 52 | const param = arg.param; |
| 53 | if (param.names.long == null and param.names.short == null) { | 53 | if (param.names.long == null and param.names.short == null) { |
| 54 | try pos.append(arg.value.?); | 54 | try pos.append(arg.value.?); |
| 55 | } else if (param.takes_value) { | 55 | } else if (param.takes_value != .None) { |
| 56 | // If we don't have any optional parameters, then this code should | 56 | // If we don't have any optional parameters, then this code should |
| 57 | // never be reached. | 57 | // never be reached. |
| 58 | debug.assert(res.options.len != 0); | 58 | debug.assert(res.options.len != 0); |
| @@ -80,7 +80,7 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 80 | 80 | ||
| 81 | pub fn flag(parser: @This(), comptime name: []const u8) bool { | 81 | pub fn flag(parser: @This(), comptime name: []const u8) bool { |
| 82 | const param = comptime findParam(name); | 82 | const param = comptime findParam(name); |
| 83 | if (param.takes_value) | 83 | if (param.takes_value != .None) |
| 84 | @compileError(name ++ " is an option and not a flag."); | 84 | @compileError(name ++ " is an option and not a flag."); |
| 85 | 85 | ||
| 86 | return parser.flags[param.id]; | 86 | return parser.flags[param.id]; |
| @@ -88,14 +88,21 @@ pub fn ComptimeClap(comptime Id: type, comptime params: []const clap.Param(Id)) | |||
| 88 | 88 | ||
| 89 | pub fn allOptions(parser: @This(), comptime name: []const u8) [][]const u8 { | 89 | pub fn allOptions(parser: @This(), comptime name: []const u8) [][]const u8 { |
| 90 | const param = comptime findParam(name); | 90 | const param = comptime findParam(name); |
| 91 | if (!param.takes_value) | 91 | if (param.takes_value == .None) |
| 92 | @compileError(name ++ " is a flag and not an option."); | 92 | @compileError(name ++ " is a flag and not an option."); |
| 93 | if (param.takes_value == .One) | ||
| 94 | @compileError(name ++ " takes one option, not multiple."); | ||
| 93 | 95 | ||
| 94 | return parser.options[param.id].items; | 96 | return parser.options[param.id].items; |
| 95 | } | 97 | } |
| 96 | 98 | ||
| 97 | pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { | 99 | pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { |
| 98 | const items = parser.allOptions(name); | 100 | const param = comptime findParam(name); |
| 101 | if (param.takes_value == .None) | ||
| 102 | @compileError(name ++ " is a flag and not an option."); | ||
| 103 | if (param.takes_value == .Many) | ||
| 104 | @compileError(name ++ " takes many options, not one."); | ||
| 105 | const items = parser.options[param.id].items; | ||
| 99 | return if (items.len > 0) items[0] else null; | 106 | return if (items.len > 0) items[0] else null; |
| 100 | } | 107 | } |
| 101 | 108 | ||
| @@ -127,9 +134,9 @@ test "clap.comptime.ComptimeClap" { | |||
| 127 | clap.parseParam("-a, --aa ") catch unreachable, | 134 | clap.parseParam("-a, --aa ") catch unreachable, |
| 128 | clap.parseParam("-b, --bb ") catch unreachable, | 135 | clap.parseParam("-b, --bb ") catch unreachable, |
| 129 | clap.parseParam("-c, --cc <V>") catch unreachable, | 136 | clap.parseParam("-c, --cc <V>") catch unreachable, |
| 130 | clap.parseParam("-d, --dd <V>") catch unreachable, | 137 | clap.parseParam("-d, --dd <V>...") catch unreachable, |
| 131 | clap.Param(clap.Help){ | 138 | clap.Param(clap.Help){ |
| 132 | .takes_value = true, | 139 | .takes_value = .One, |
| 133 | }, | 140 | }, |
| 134 | }); | 141 | }); |
| 135 | 142 | ||
| @@ -151,6 +158,6 @@ test "clap.comptime.ComptimeClap" { | |||
| 151 | testing.expectEqualStrings("0", args.option("--cc").?); | 158 | testing.expectEqualStrings("0", args.option("--cc").?); |
| 152 | testing.expectEqual(@as(usize, 1), args.positionals().len); | 159 | testing.expectEqual(@as(usize, 1), args.positionals().len); |
| 153 | testing.expectEqualStrings("something", args.positionals()[0]); | 160 | testing.expectEqualStrings("something", args.positionals()[0]); |
| 154 | testing.expectEqualStrings("a", args.option("-d").?); | 161 | testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.allOptions("-d")); |
| 155 | testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.allOptions("--dd")); | 162 | testing.expectEqualSlices([]const u8, &[_][]const u8{ "a", "b" }, args.allOptions("--dd")); |
| 156 | } | 163 | } |