diff options
| author | 2018-06-20 10:18:56 +0200 | |
|---|---|---|
| committer | 2018-06-20 10:18:56 +0200 | |
| commit | 01096b538ab3925cfbc18e14b9321a26eb54d552 (patch) | |
| tree | c8f5738dae689f2a5267423b2bac705289dd47d5 | |
| parent | Updated to newest version of zig (diff) | |
| download | zig-clap-01096b538ab3925cfbc18e14b9321a26eb54d552.tar.gz zig-clap-01096b538ab3925cfbc18e14b9321a26eb54d552.tar.xz zig-clap-01096b538ab3925cfbc18e14b9321a26eb54d552.zip | |
Updated to use pass-by-value where possible
| -rw-r--r-- | src/core.zig | 6 | ||||
| -rw-r--r-- | src/extended.zig | 27 | ||||
| -rw-r--r-- | tests/extended.zig | 16 |
3 files changed, 25 insertions, 24 deletions
diff --git a/src/core.zig b/src/core.zig index a0f794c..bdd1bf4 100644 --- a/src/core.zig +++ b/src/core.zig | |||
| @@ -99,7 +99,7 @@ pub fn Param(comptime Id: type) type { | |||
| 99 | takes_value: bool, | 99 | takes_value: bool, |
| 100 | names: Names, | 100 | names: Names, |
| 101 | 101 | ||
| 102 | pub fn init(id: Id, takes_value: bool, names: *const Names) Self { | 102 | pub fn init(id: Id, takes_value: bool, names: Names) Self { |
| 103 | // Assert, that if the param have no name, then it has to take | 103 | // Assert, that if the param have no name, then it has to take |
| 104 | // a value. | 104 | // a value. |
| 105 | debug.assert(names.bare != null or | 105 | debug.assert(names.bare != null or |
| @@ -110,7 +110,7 @@ pub fn Param(comptime Id: type) type { | |||
| 110 | return Self{ | 110 | return Self{ |
| 111 | .id = id, | 111 | .id = id, |
| 112 | .takes_value = takes_value, | 112 | .takes_value = takes_value, |
| 113 | .names = names.*, | 113 | .names = names, |
| 114 | }; | 114 | }; |
| 115 | } | 115 | } |
| 116 | }; | 116 | }; |
| @@ -331,7 +331,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { | |||
| 331 | } | 331 | } |
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | fn chainging(clap: *Self, state: *const State.Chaining) !?Arg(Id) { | 334 | fn chainging(clap: *Self, state: State.Chaining) !?Arg(Id) { |
| 335 | const arg = state.arg; | 335 | const arg = state.arg; |
| 336 | const index = state.index; | 336 | const index = state.index; |
| 337 | const next_index = index + 1; | 337 | const next_index = index + 1; |
diff --git a/src/extended.zig b/src/extended.zig index 09e91dd..f7fc87d 100644 --- a/src/extended.zig +++ b/src/extended.zig | |||
| @@ -13,13 +13,12 @@ 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, | ||
| 17 | kind: Kind, | 16 | kind: Kind, |
| 18 | 17 | ||
| 19 | required: bool, | 18 | required: bool, |
| 20 | position: ?usize, | 19 | position: ?usize, |
| 21 | 20 | ||
| 22 | pub fn flag(field: []const u8, names: *const core.Names) Param { | 21 | pub fn flag(field: []const u8, names: core.Names) Param { |
| 23 | return init( | 22 | return init( |
| 24 | field, | 23 | field, |
| 25 | names, | 24 | names, |
| @@ -29,33 +28,33 @@ pub const Param = struct { | |||
| 29 | 28 | ||
| 30 | pub fn option( | 29 | pub fn option( |
| 31 | field: []const u8, | 30 | field: []const u8, |
| 32 | names: *const core.Names, | 31 | names: core.Names, |
| 33 | comptime parser: *const Parser, | 32 | comptime parser: Parser, |
| 34 | ) Param { | 33 | ) Param { |
| 35 | return init( | 34 | return init( |
| 36 | field, | 35 | field, |
| 37 | names, | 36 | names, |
| 38 | Kind{ .Option = parser.* }, | 37 | Kind{ .Option = parser }, |
| 39 | ); | 38 | ); |
| 40 | } | 39 | } |
| 41 | 40 | ||
| 42 | pub fn subcommand( | 41 | pub fn subcommand( |
| 43 | field: []const u8, | 42 | field: []const u8, |
| 44 | names: *const core.Names, | 43 | names: core.Names, |
| 45 | comptime command: *const Command, | 44 | comptime command: Command, |
| 46 | ) Param { | 45 | ) Param { |
| 47 | return init( | 46 | return init( |
| 48 | field, | 47 | field, |
| 49 | names, | 48 | names, |
| 50 | Kind{ .Subcommand = command.* }, | 49 | Kind{ .Subcommand = command }, |
| 51 | ); | 50 | ); |
| 52 | } | 51 | } |
| 53 | 52 | ||
| 54 | pub fn init(field: []const u8, names: *const core.Names, kind: *const Kind) Param { | 53 | pub fn init(field: []const u8, names: core.Names, kind: Kind) Param { |
| 55 | return Param{ | 54 | return Param{ |
| 56 | .field = field, | 55 | .field = field, |
| 57 | .names = names.*, | 56 | .names = names, |
| 58 | .kind = kind.*, | 57 | .kind = kind, |
| 59 | .required = false, | 58 | .required = false, |
| 60 | .position = null, | 59 | .position = null, |
| 61 | }; | 60 | }; |
| @@ -130,6 +129,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 130 | default: Result, | 129 | default: Result, |
| 131 | params: []const Param, | 130 | params: []const Param, |
| 132 | 131 | ||
| 132 | // TODO: pass-by-value | ||
| 133 | pub fn parse( | 133 | pub fn parse( |
| 134 | comptime clap: *const Self, | 134 | comptime clap: *const Self, |
| 135 | comptime Error: type, | 135 | comptime Error: type, |
| @@ -142,6 +142,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 142 | return try parseHelper(&top_level_command, Error, &c); | 142 | return try parseHelper(&top_level_command, Error, &c); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | // TODO: pass-by-value | ||
| 145 | fn parseHelper( | 146 | fn parseHelper( |
| 146 | comptime command: *const Command, | 147 | comptime command: *const Command, |
| 147 | comptime Error: type, | 148 | comptime Error: type, |
| @@ -152,7 +153,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 152 | var handled = comptime blk: { | 153 | var handled = comptime blk: { |
| 153 | var res: [command.params.len]bool = undefined; | 154 | var res: [command.params.len]bool = undefined; |
| 154 | for (command.params) |p, i| { | 155 | for (command.params) |p, i| { |
| 155 | res[i] = !p.settings.required; | 156 | res[i] = !p.required; |
| 156 | } | 157 | } |
| 157 | 158 | ||
| 158 | break :blk res; | 159 | break :blk res; |
| @@ -180,7 +181,7 @@ pub fn Clap(comptime Result: type) type { | |||
| 180 | 181 | ||
| 181 | arg_loop: while (try clap.next()) |arg| : (pos += 1) { | 182 | arg_loop: while (try clap.next()) |arg| : (pos += 1) { |
| 182 | inline for (command.params) |param, i| { | 183 | inline for (command.params) |param, i| { |
| 183 | if (arg.param.id == i and (param.settings.position orelse pos) == pos) { | 184 | if (arg.param.id == i and (param.position orelse pos) == pos) { |
| 184 | handled[i] = true; | 185 | handled[i] = true; |
| 185 | 186 | ||
| 186 | switch (param.kind) { | 187 | switch (param.kind) { |
diff --git a/tests/extended.zig b/tests/extended.zig index 3dbb87d..9670814 100644 --- a/tests/extended.zig +++ b/tests/extended.zig | |||
| @@ -26,10 +26,10 @@ pub fn Test(comptime Expect: type) type { | |||
| 26 | Fail: error, | 26 | Fail: error, |
| 27 | }; | 27 | }; |
| 28 | 28 | ||
| 29 | pub fn success(args: []const []const u8, expected: *const Expect) Self { | 29 | pub fn success(args: []const []const u8, expected: Expect) Self { |
| 30 | return Self{ | 30 | return Self{ |
| 31 | .args = args, | 31 | .args = args, |
| 32 | .kind = Kind{ .Success = expected.* }, | 32 | .kind = Kind{ .Success = expected }, |
| 33 | }; | 33 | }; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| @@ -40,7 +40,7 @@ pub fn Test(comptime Expect: type) type { | |||
| 40 | }; | 40 | }; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | pub fn run(t: *const Self, comptime parser: var) void { | 43 | pub fn run(t: Self, comptime parser: var) void { |
| 44 | var iter = ArgSliceIterator.init(t.args); | 44 | var iter = ArgSliceIterator.init(t.args); |
| 45 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); | 45 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); |
| 46 | 46 | ||
| @@ -81,7 +81,7 @@ test "clap.extended: short" { | |||
| 81 | res.position = 0; | 81 | res.position = 0; |
| 82 | break :p res; | 82 | break :p res; |
| 83 | }, | 83 | }, |
| 84 | Param.option("b", Names.short('b'), &Parser.int(u8, 10)), | 84 | Param.option("b", Names.short('b'), Parser.int(u8, 10)), |
| 85 | }, | 85 | }, |
| 86 | }; | 86 | }; |
| 87 | 87 | ||
| @@ -172,12 +172,12 @@ test "clap.extended: long" { | |||
| 172 | }, | 172 | }, |
| 173 | .params = []Param{ | 173 | .params = []Param{ |
| 174 | p: { | 174 | p: { |
| 175 | var res = Param.long("a", Names.short('a')); | 175 | var res = Param.flag("a", Names.long("a")); |
| 176 | res.required = true; | 176 | res.required = true; |
| 177 | res.position = 0; | 177 | res.position = 0; |
| 178 | break :p res; | 178 | break :p res; |
| 179 | }, | 179 | }, |
| 180 | Param.option("b", Names.long('b'), &Parser.int(u8, 10)), | 180 | Param.option("b", Names.long("b"), Parser.int(u8, 10)), |
| 181 | }, | 181 | }, |
| 182 | }; | 182 | }; |
| 183 | 183 | ||
| @@ -244,12 +244,12 @@ test "clap.extended: bare" { | |||
| 244 | }, | 244 | }, |
| 245 | .params = []Param{ | 245 | .params = []Param{ |
| 246 | p: { | 246 | p: { |
| 247 | var res = Param.bare("a", Names.short('a')); | 247 | var res = Param.flag("a", Names.bare("a")); |
| 248 | res.required = true; | 248 | res.required = true; |
| 249 | res.position = 0; | 249 | res.position = 0; |
| 250 | break :p res; | 250 | break :p res; |
| 251 | }, | 251 | }, |
| 252 | Param.option("b", Names.bare('b'), &Parser.int(u8, 10)), | 252 | Param.option("b", Names.bare("b"), Parser.int(u8, 10)), |
| 253 | }, | 253 | }, |
| 254 | }; | 254 | }; |
| 255 | 255 | ||