diff options
| author | 2018-06-08 10:58:25 +0200 | |
|---|---|---|
| committer | 2018-06-08 10:58:25 +0200 | |
| commit | 4411c9c5c6c96559b69b5ca8378c72748c5d0842 (patch) | |
| tree | 55f271ddc9c7fce6460e8dda6a87d334d524addd | |
| parent | Updated to newest pointer syntax (diff) | |
| download | zig-clap-4411c9c5c6c96559b69b5ca8378c72748c5d0842.tar.gz zig-clap-4411c9c5c6c96559b69b5ca8378c72748c5d0842.tar.xz zig-clap-4411c9c5c6c96559b69b5ca8378c72748c5d0842.zip | |
Added helper functions to construct extended.Param
| -rw-r--r-- | src/extended.zig | 60 | ||||
| -rw-r--r-- | tests/extended.zig | 45 |
2 files changed, 63 insertions, 42 deletions
diff --git a/src/extended.zig b/src/extended.zig index 8fb017f..5fd019d 100644 --- a/src/extended.zig +++ b/src/extended.zig | |||
| @@ -13,14 +13,62 @@ const assert = debug.assert; | |||
| 13 | pub const Param = struct { | 13 | pub const Param = struct { |
| 14 | field: []const u8, | 14 | field: []const u8, |
| 15 | names: core.Names, | 15 | names: core.Names, |
| 16 | settings: Settings, | ||
| 16 | kind: Kind, | 17 | kind: Kind, |
| 17 | required: bool, | 18 | |
| 18 | position: ?usize, | 19 | pub fn flag(field: []const u8, names: *const core.Names, settings: *const Settings) Param { |
| 20 | return Param{ | ||
| 21 | .field = field, | ||
| 22 | .names = names.*, | ||
| 23 | .settings = settings.*, | ||
| 24 | .kind = Kind.Flag, | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | |||
| 28 | pub fn option( | ||
| 29 | field: []const u8, | ||
| 30 | names: *const core.Names, | ||
| 31 | settings: *const Settings, | ||
| 32 | comptime parser: *const Parser, | ||
| 33 | ) Param { | ||
| 34 | return Param{ | ||
| 35 | .field = field, | ||
| 36 | .names = names.*, | ||
| 37 | .settings = settings.*, | ||
| 38 | .kind = Kind{ .Option = parser.* }, | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | pub fn subcommand( | ||
| 43 | field: []const u8, | ||
| 44 | names: *const core.Names, | ||
| 45 | settings: *const Settings, | ||
| 46 | comptime command: *const Command, | ||
| 47 | ) Param { | ||
| 48 | return Param{ | ||
| 49 | .field = field, | ||
| 50 | .names = names.*, | ||
| 51 | .settings = settings.*, | ||
| 52 | .kind = Kind{ .Subcommand = Command.* }, | ||
| 53 | }; | ||
| 54 | } | ||
| 19 | 55 | ||
| 20 | pub const Kind = union(enum) { | 56 | pub const Kind = union(enum) { |
| 21 | Flag, | 57 | Flag, |
| 22 | Option: Parser, | 58 | Option: Parser, |
| 23 | SubCommand: Command, | 59 | Subcommand: Command, |
| 60 | }; | ||
| 61 | |||
| 62 | pub const Settings = struct { | ||
| 63 | required: bool, | ||
| 64 | position: ?usize, | ||
| 65 | |||
| 66 | pub fn default() Settings { | ||
| 67 | return Settings{ | ||
| 68 | .required = false, | ||
| 69 | .position = null, | ||
| 70 | }; | ||
| 71 | } | ||
| 24 | }; | 72 | }; |
| 25 | }; | 73 | }; |
| 26 | 74 | ||
| @@ -116,7 +164,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 116 | var handled = comptime blk: { | 164 | var handled = comptime blk: { |
| 117 | var res: [command.params.len]bool = undefined; | 165 | var res: [command.params.len]bool = undefined; |
| 118 | for (command.params) |p, i| { | 166 | for (command.params) |p, i| { |
| 119 | res[i] = !p.required; | 167 | res[i] = !p.settings.required; |
| 120 | } | 168 | } |
| 121 | 169 | ||
| 122 | break :blk res; | 170 | break :blk res; |
| @@ -145,7 +193,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 145 | arg_loop: | 193 | arg_loop: |
| 146 | while (try clap.next()) |arg| : (pos += 1) { | 194 | while (try clap.next()) |arg| : (pos += 1) { |
| 147 | inline for(command.params) |param, i| { | 195 | inline for(command.params) |param, i| { |
| 148 | if (arg.param.id == i and (param.position ?? pos) == pos) { | 196 | if (arg.param.id == i and (param.settings.position ?? pos) == pos) { |
| 149 | handled[i] = true; | 197 | handled[i] = true; |
| 150 | 198 | ||
| 151 | switch (param.kind) { | 199 | switch (param.kind) { |
| @@ -155,7 +203,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 155 | Param.Kind.Option => |parser| { | 203 | Param.Kind.Option => |parser| { |
| 156 | try parser.parse(getFieldPtr(&result, param.field), ??arg.value); | 204 | try parser.parse(getFieldPtr(&result, param.field), ??arg.value); |
| 157 | }, | 205 | }, |
| 158 | Param.Kind.SubCommand => |sub_command| { | 206 | Param.Kind.Subcommand => |sub_command| { |
| 159 | getFieldPtr(&result, param.field).* = try sub_command.parseHelper(Error, clap); | 207 | getFieldPtr(&result, param.field).* = try sub_command.parseHelper(Error, clap); |
| 160 | 208 | ||
| 161 | // After parsing a subcommand, there should be no arguments left. | 209 | // After parsing a subcommand, there should be no arguments left. |
diff --git a/tests/extended.zig b/tests/extended.zig index 00cc84b..3af2025 100644 --- a/tests/extended.zig +++ b/tests/extended.zig | |||
| @@ -98,20 +98,11 @@ test "clap.extended: short" { | |||
| 98 | .b = 0, | 98 | .b = 0, |
| 99 | }, | 99 | }, |
| 100 | .params = []Param{ | 100 | .params = []Param{ |
| 101 | Param{ | 101 | Param.flag("a", Names.short('a'), Param.Settings{ |
| 102 | .field = "a", | ||
| 103 | .names = Names.short('a'), | ||
| 104 | .kind = Param.Kind.Flag, | ||
| 105 | .required = true, | 102 | .required = true, |
| 106 | .position = 0, | 103 | .position = 0, |
| 107 | }, | 104 | }), |
| 108 | Param{ | 105 | Param.option("b", Names.short('b'), Param.Settings.default(), &Parser.int(u8, 10)), |
| 109 | .field = "b", | ||
| 110 | .names = Names.short('b'), | ||
| 111 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 112 | .required = false, | ||
| 113 | .position = null, | ||
| 114 | }, | ||
| 115 | } | 106 | } |
| 116 | }; | 107 | }; |
| 117 | 108 | ||
| @@ -201,20 +192,11 @@ test "clap.extended: long" { | |||
| 201 | .b = 0, | 192 | .b = 0, |
| 202 | }, | 193 | }, |
| 203 | .params = []Param{ | 194 | .params = []Param{ |
| 204 | Param{ | 195 | Param.flag("a", Names.long("a"), Param.Settings{ |
| 205 | .field = "a", | ||
| 206 | .names = Names.long("a"), | ||
| 207 | .kind = Param.Kind.Flag, | ||
| 208 | .required = true, | 196 | .required = true, |
| 209 | .position = 0, | 197 | .position = 0, |
| 210 | }, | 198 | }), |
| 211 | Param{ | 199 | Param.option("b", Names.long("b"), Param.Settings.default(), &Parser.int(u8, 10)), |
| 212 | .field = "b", | ||
| 213 | .names = Names.long("b"), | ||
| 214 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 215 | .required = false, | ||
| 216 | .position = null, | ||
| 217 | }, | ||
| 218 | } | 200 | } |
| 219 | }; | 201 | }; |
| 220 | 202 | ||
| @@ -280,20 +262,11 @@ test "clap.extended: bare" { | |||
| 280 | .b = 0, | 262 | .b = 0, |
| 281 | }, | 263 | }, |
| 282 | .params = []Param{ | 264 | .params = []Param{ |
| 283 | Param{ | 265 | Param.flag("a", Names.bare("a"), Param.Settings{ |
| 284 | .field = "a", | ||
| 285 | .names = Names.bare("a"), | ||
| 286 | .kind = Param.Kind.Flag, | ||
| 287 | .required = true, | 266 | .required = true, |
| 288 | .position = 0, | 267 | .position = 0, |
| 289 | }, | 268 | }), |
| 290 | Param{ | 269 | Param.option("b", Names.bare("b"), Param.Settings.default(), &Parser.int(u8, 10)), |
| 291 | .field = "b", | ||
| 292 | .names = Names.bare("b"), | ||
| 293 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 294 | .required = false, | ||
| 295 | .position = null, | ||
| 296 | }, | ||
| 297 | } | 270 | } |
| 298 | }; | 271 | }; |
| 299 | 272 | ||