diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.zig | 6 | ||||
| -rw-r--r-- | src/extended.zig | 27 |
2 files changed, 17 insertions, 16 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) { |