diff options
| author | 2020-11-10 18:54:22 +0100 | |
|---|---|---|
| committer | 2020-11-10 18:54:22 +0100 | |
| commit | 6b361546e2341a37cfb9f8e35585db524937a589 (patch) | |
| tree | f979a760149f4c7a2d9b9113a872d8d7e6d56bef | |
| parent | Update ci to use 0.7.0 (diff) | |
| parent | Deprecate ComptimeClap in favor of parseEx (diff) | |
| download | zig-clap-6b361546e2341a37cfb9f8e35585db524937a589.tar.gz zig-clap-6b361546e2341a37cfb9f8e35585db524937a589.tar.xz zig-clap-6b361546e2341a37cfb9f8e35585db524937a589.zip | |
Merge branch 'master' into zig-master
| -rw-r--r-- | README.md | 56 | ||||
| -rw-r--r-- | build.zig | 3 | ||||
| -rw-r--r-- | clap.zig | 21 | ||||
| -rw-r--r-- | clap/comptime.zig | 8 | ||||
| -rw-r--r-- | example/README.md.template | 11 | ||||
| -rw-r--r-- | example/simple-ex.zig (renamed from example/comptime-clap.zig) | 4 |
6 files changed, 26 insertions, 77 deletions
| @@ -99,59 +99,7 @@ zig-clap/example/simple-error.zig:16:18: note: called from here | |||
| 99 | _ = args.flag("--helps"); | 99 | _ = args.flag("--helps"); |
| 100 | ``` | 100 | ``` |
| 101 | 101 | ||
| 102 | ### `ComptimeClap` | 102 | There is also a `parseEx` variant that takes an argument iterator. |
| 103 | |||
| 104 | The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument | ||
| 105 | iterator. | ||
| 106 | |||
| 107 | ```zig | ||
| 108 | const std = @import("std"); | ||
| 109 | const clap = @import("clap"); | ||
| 110 | |||
| 111 | const debug = std.debug; | ||
| 112 | |||
| 113 | pub fn main() !void { | ||
| 114 | const allocator = std.heap.page_allocator; | ||
| 115 | |||
| 116 | // First we specify what parameters our program can take. | ||
| 117 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 118 | const params = comptime [_]clap.Param(clap.Help){ | ||
| 119 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | ||
| 120 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | ||
| 121 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | ||
| 122 | clap.parseParam("<POS>...") catch unreachable, | ||
| 123 | }; | ||
| 124 | const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); | ||
| 125 | |||
| 126 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | ||
| 127 | // wraps iterating over arguments the most efficient way on each os. | ||
| 128 | var iter = try clap.args.OsIterator.init(allocator); | ||
| 129 | defer iter.deinit(); | ||
| 130 | |||
| 131 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 132 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 133 | // don't care about the extra information `Diagnostics` provides. | ||
| 134 | var diag: clap.Diagnostic = undefined; | ||
| 135 | |||
| 136 | // Parse the arguments | ||
| 137 | var args = Clap.parse(allocator, &iter, &diag) catch |err| { | ||
| 138 | // Report useful error and exit | ||
| 139 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | ||
| 140 | return err; | ||
| 141 | }; | ||
| 142 | defer args.deinit(); | ||
| 143 | |||
| 144 | if (args.flag("--help")) | ||
| 145 | debug.warn("--help\n", .{}); | ||
| 146 | if (args.option("--number")) |n| | ||
| 147 | debug.warn("--number = {}\n", .{n}); | ||
| 148 | for (args.options("--string")) |s| | ||
| 149 | debug.warn("--string = {}\n", .{s}); | ||
| 150 | for (args.positionals()) |pos| | ||
| 151 | debug.warn("{}\n", .{pos}); | ||
| 152 | } | ||
| 153 | |||
| 154 | ``` | ||
| 155 | 103 | ||
| 156 | ### `StreamingClap` | 104 | ### `StreamingClap` |
| 157 | 105 | ||
| @@ -222,7 +170,7 @@ pub fn main() !void { | |||
| 222 | 170 | ||
| 223 | ``` | 171 | ``` |
| 224 | 172 | ||
| 225 | Currently, this parse is the only parser that allow an array of `Param` that | 173 | Currently, this parse is the only parser that allow an array of `Param` tha |
| 226 | is generated at runtime. | 174 | is generated at runtime. |
| 227 | 175 | ||
| 228 | ### `help` | 176 | ### `help` |
| @@ -32,8 +32,8 @@ pub fn build(b: *Builder) void { | |||
| 32 | const example_step = b.step("examples", "Build examples"); | 32 | const example_step = b.step("examples", "Build examples"); |
| 33 | inline for ([_][]const u8{ | 33 | inline for ([_][]const u8{ |
| 34 | "simple", | 34 | "simple", |
| 35 | "simple-ex", | ||
| 35 | //"simple-error", | 36 | //"simple-error", |
| 36 | "comptime-clap", | ||
| 37 | "streaming-clap", | 37 | "streaming-clap", |
| 38 | "help", | 38 | "help", |
| 39 | "usage", | 39 | "usage", |
| @@ -70,7 +70,6 @@ fn readMeStep(b: *Builder) *std.build.Step { | |||
| 70 | try stream.print(@embedFile("example/README.md.template"), .{ | 70 | try stream.print(@embedFile("example/README.md.template"), .{ |
| 71 | @embedFile("example/simple.zig"), | 71 | @embedFile("example/simple.zig"), |
| 72 | @embedFile("example/simple-error.zig"), | 72 | @embedFile("example/simple-error.zig"), |
| 73 | @embedFile("example/comptime-clap.zig"), | ||
| 74 | @embedFile("example/streaming-clap.zig"), | 73 | @embedFile("example/streaming-clap.zig"), |
| 75 | @embedFile("example/help.zig"), | 74 | @embedFile("example/help.zig"), |
| 76 | @embedFile("example/usage.zig"), | 75 | @embedFile("example/usage.zig"), |
| @@ -303,7 +303,7 @@ test "Diagnostic.report" { | |||
| 303 | pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | 303 | pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { |
| 304 | return struct { | 304 | return struct { |
| 305 | arena: std.heap.ArenaAllocator, | 305 | arena: std.heap.ArenaAllocator, |
| 306 | clap: ComptimeClap(Id, args.OsIterator, params), | 306 | clap: ComptimeClap(Id, params), |
| 307 | exe_arg: ?[]const u8, | 307 | exe_arg: ?[]const u8, |
| 308 | 308 | ||
| 309 | pub fn deinit(a: *@This()) void { | 309 | pub fn deinit(a: *@This()) void { |
| @@ -329,8 +329,7 @@ pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | |||
| 329 | }; | 329 | }; |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | /// Parses the command line arguments passed into the program based on an | 332 | /// Same as `parseEx` but uses the `args.OsIterator` by default. |
| 333 | /// array of `Param`s. | ||
| 334 | pub fn parse( | 333 | pub fn parse( |
| 335 | comptime Id: type, | 334 | comptime Id: type, |
| 336 | comptime params: []const Param(Id), | 335 | comptime params: []const Param(Id), |
| @@ -338,8 +337,7 @@ pub fn parse( | |||
| 338 | diag: ?*Diagnostic, | 337 | diag: ?*Diagnostic, |
| 339 | ) !Args(Id, params) { | 338 | ) !Args(Id, params) { |
| 340 | var iter = try args.OsIterator.init(allocator); | 339 | var iter = try args.OsIterator.init(allocator); |
| 341 | const Clap = ComptimeClap(Id, args.OsIterator, params); | 340 | const clap = try parseEx(Id, params, allocator, &iter, diag); |
| 342 | const clap = try Clap.parse(allocator, &iter, diag); | ||
| 343 | return Args(Id, params){ | 341 | return Args(Id, params){ |
| 344 | .arena = iter.arena, | 342 | .arena = iter.arena, |
| 345 | .clap = clap, | 343 | .clap = clap, |
| @@ -347,6 +345,19 @@ pub fn parse( | |||
| 347 | }; | 345 | }; |
| 348 | } | 346 | } |
| 349 | 347 | ||
| 348 | /// Parses the command line arguments passed into the program based on an | ||
| 349 | /// array of `Param`s. | ||
| 350 | pub fn parseEx( | ||
| 351 | comptime Id: type, | ||
| 352 | comptime params: []const Param(Id), | ||
| 353 | allocator: *mem.Allocator, | ||
| 354 | iter: anytype, | ||
| 355 | diag: ?*Diagnostic, | ||
| 356 | ) !ComptimeClap(Id, params) { | ||
| 357 | const Clap = ComptimeClap(Id, params); | ||
| 358 | return try Clap.parse(allocator, iter, diag); | ||
| 359 | } | ||
| 360 | |||
| 350 | /// Will print a help message in the following format: | 361 | /// Will print a help message in the following format: |
| 351 | /// -s, --long <valueText> helpText | 362 | /// -s, --long <valueText> helpText |
| 352 | /// -s, helpText | 363 | /// -s, helpText |
diff --git a/clap/comptime.zig b/clap/comptime.zig index 80eb428..8ab61cb 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -6,9 +6,9 @@ const heap = std.heap; | |||
| 6 | const mem = std.mem; | 6 | const mem = std.mem; |
| 7 | const debug = std.debug; | 7 | const debug = std.debug; |
| 8 | 8 | ||
| 9 | /// Deprecated: Use `parseEx` instead | ||
| 9 | pub fn ComptimeClap( | 10 | pub fn ComptimeClap( |
| 10 | comptime Id: type, | 11 | comptime Id: type, |
| 11 | comptime ArgIter: type, | ||
| 12 | comptime params: []const clap.Param(Id), | 12 | comptime params: []const clap.Param(Id), |
| 13 | ) type { | 13 | ) type { |
| 14 | var flags: usize = 0; | 14 | var flags: usize = 0; |
| @@ -42,7 +42,7 @@ pub fn ComptimeClap( | |||
| 42 | pos: []const []const u8, | 42 | pos: []const []const u8, |
| 43 | allocator: *mem.Allocator, | 43 | allocator: *mem.Allocator, |
| 44 | 44 | ||
| 45 | pub fn parse(allocator: *mem.Allocator, iter: *ArgIter, diag: ?*clap.Diagnostic) !@This() { | 45 | pub fn parse(allocator: *mem.Allocator, iter: anytype, diag: ?*clap.Diagnostic) !@This() { |
| 46 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; | 46 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; |
| 47 | for (multis) |*multi| { | 47 | for (multis) |*multi| { |
| 48 | multi.* = std.ArrayList([]const u8).init(allocator); | 48 | multi.* = std.ArrayList([]const u8).init(allocator); |
| @@ -58,7 +58,7 @@ pub fn ComptimeClap( | |||
| 58 | .allocator = allocator, | 58 | .allocator = allocator, |
| 59 | }; | 59 | }; |
| 60 | 60 | ||
| 61 | var stream = clap.StreamingClap(usize, ArgIter){ | 61 | var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){ |
| 62 | .params = converted_params, | 62 | .params = converted_params, |
| 63 | .iter = iter, | 63 | .iter = iter, |
| 64 | }; | 64 | }; |
| @@ -147,7 +147,7 @@ pub fn ComptimeClap( | |||
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | test "" { | 149 | test "" { |
| 150 | const Clap = ComptimeClap(clap.Help, clap.args.SliceIterator, comptime &[_]clap.Param(clap.Help){ | 150 | const Clap = ComptimeClap(clap.Help, comptime &[_]clap.Param(clap.Help){ |
| 151 | clap.parseParam("-a, --aa ") catch unreachable, | 151 | clap.parseParam("-a, --aa ") catch unreachable, |
| 152 | clap.parseParam("-b, --bb ") catch unreachable, | 152 | clap.parseParam("-b, --bb ") catch unreachable, |
| 153 | clap.parseParam("-c, --cc <V>") catch unreachable, | 153 | clap.parseParam("-c, --cc <V>") catch unreachable, |
diff --git a/example/README.md.template b/example/README.md.template index 65b507d..530cea4 100644 --- a/example/README.md.template +++ b/example/README.md.template | |||
| @@ -50,14 +50,7 @@ zig-clap/example/simple-error.zig:16:18: note: called from here | |||
| 50 | _ = args.flag("--helps"); | 50 | _ = args.flag("--helps"); |
| 51 | ``` | 51 | ``` |
| 52 | 52 | ||
| 53 | ### `ComptimeClap` | 53 | There is also a `parseEx` variant that takes an argument iterator. |
| 54 | |||
| 55 | The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument | ||
| 56 | iterator. | ||
| 57 | |||
| 58 | ```zig | ||
| 59 | {} | ||
| 60 | ``` | ||
| 61 | 54 | ||
| 62 | ### `StreamingClap` | 55 | ### `StreamingClap` |
| 63 | 56 | ||
| @@ -68,7 +61,7 @@ The `StreamingClap` is the base of all the other parsers. It's a streaming parse | |||
| 68 | {} | 61 | {} |
| 69 | ``` | 62 | ``` |
| 70 | 63 | ||
| 71 | Currently, this parse is the only parser that allow an array of `Param` that | 64 | Currently, this parse is the only parser that allow an array of `Param` tha |
| 72 | is generated at runtime. | 65 | is generated at runtime. |
| 73 | 66 | ||
| 74 | ### `help` | 67 | ### `help` |
diff --git a/example/comptime-clap.zig b/example/simple-ex.zig index e5d02ff..d6ecc44 100644 --- a/example/comptime-clap.zig +++ b/example/simple-ex.zig | |||
| @@ -14,7 +14,6 @@ pub fn main() !void { | |||
| 14 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | 14 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, |
| 15 | clap.parseParam("<POS>...") catch unreachable, | 15 | clap.parseParam("<POS>...") catch unreachable, |
| 16 | }; | 16 | }; |
| 17 | const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); | ||
| 18 | 17 | ||
| 19 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 18 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| 20 | // wraps iterating over arguments the most efficient way on each os. | 19 | // wraps iterating over arguments the most efficient way on each os. |
| @@ -26,8 +25,7 @@ pub fn main() !void { | |||
| 26 | // don't care about the extra information `Diagnostics` provides. | 25 | // don't care about the extra information `Diagnostics` provides. |
| 27 | var diag: clap.Diagnostic = undefined; | 26 | var diag: clap.Diagnostic = undefined; |
| 28 | 27 | ||
| 29 | // Parse the arguments | 28 | var args = clap.parseEx(clap.Help, ¶ms, allocator, &iter, &diag) catch |err| { |
| 30 | var args = Clap.parse(allocator, &iter, &diag) catch |err| { | ||
| 31 | // Report useful error and exit | 29 | // Report useful error and exit |
| 32 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 30 | diag.report(std.io.getStdErr().outStream(), err) catch {}; |
| 33 | return err; | 31 | return err; |