From b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Tue, 10 Nov 2020 18:53:57 +0100 Subject: Deprecate ComptimeClap in favor of parseEx --- example/README.md.template | 11 ++--------- example/comptime-clap.zig | 45 --------------------------------------------- example/simple-ex.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 54 deletions(-) delete mode 100644 example/comptime-clap.zig create mode 100644 example/simple-ex.zig (limited to 'example') 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 _ = args.flag("--helps"); ``` -### `ComptimeClap` - -The `ComptimeClap` is the parser used by `clap.parse`. It allows the user to use a custom argument -iterator. - -```zig -{} -``` +There is also a `parseEx` variant that takes an argument iterator. ### `StreamingClap` @@ -68,7 +61,7 @@ The `StreamingClap` is the base of all the other parsers. It's a streaming parse {} ``` -Currently, this parse is the only parser that allow an array of `Param` that +Currently, this parse is the only parser that allow an array of `Param` tha is generated at runtime. ### `help` diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig deleted file mode 100644 index e5d02ff..0000000 --- a/example/comptime-clap.zig +++ /dev/null @@ -1,45 +0,0 @@ -const std = @import("std"); -const clap = @import("clap"); - -const debug = std.debug; - -pub fn main() !void { - const allocator = std.heap.page_allocator; - - // First we specify what parameters our program can take. - // We can use `parseParam` to parse a string to a `Param(Help)` - const params = comptime [_]clap.Param(clap.Help){ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-n, --number An option parameter, which takes a value.") catch unreachable, - clap.parseParam("-s, --string ... An option parameter which can be specified multiple times.") catch unreachable, - clap.parseParam("...") catch unreachable, - }; - const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); - - // We then initialize an argument iterator. We will use the OsIterator as it nicely - // wraps iterating over arguments the most efficient way on each os. - var iter = try clap.args.OsIterator.init(allocator); - defer iter.deinit(); - - // Initalize our diagnostics, which can be used for reporting useful errors. - // This is optional. You can also just pass `null` to `parser.next` if you - // don't care about the extra information `Diagnostics` provides. - var diag: clap.Diagnostic = undefined; - - // Parse the arguments - var args = Clap.parse(allocator, &iter, &diag) catch |err| { - // Report useful error and exit - diag.report(std.io.getStdErr().outStream(), err) catch {}; - return err; - }; - defer args.deinit(); - - if (args.flag("--help")) - debug.warn("--help\n", .{}); - if (args.option("--number")) |n| - debug.warn("--number = {}\n", .{n}); - for (args.options("--string")) |s| - debug.warn("--string = {}\n", .{s}); - for (args.positionals()) |pos| - debug.warn("{}\n", .{pos}); -} diff --git a/example/simple-ex.zig b/example/simple-ex.zig new file mode 100644 index 0000000..d6ecc44 --- /dev/null +++ b/example/simple-ex.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const clap = @import("clap"); + +const debug = std.debug; + +pub fn main() !void { + const allocator = std.heap.page_allocator; + + // First we specify what parameters our program can take. + // We can use `parseParam` to parse a string to a `Param(Help)` + const params = comptime [_]clap.Param(clap.Help){ + clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, + clap.parseParam("-n, --number An option parameter, which takes a value.") catch unreachable, + clap.parseParam("-s, --string ... An option parameter which can be specified multiple times.") catch unreachable, + clap.parseParam("...") catch unreachable, + }; + + // We then initialize an argument iterator. We will use the OsIterator as it nicely + // wraps iterating over arguments the most efficient way on each os. + var iter = try clap.args.OsIterator.init(allocator); + defer iter.deinit(); + + // Initalize our diagnostics, which can be used for reporting useful errors. + // This is optional. You can also just pass `null` to `parser.next` if you + // don't care about the extra information `Diagnostics` provides. + var diag: clap.Diagnostic = undefined; + + var args = clap.parseEx(clap.Help, ¶ms, allocator, &iter, &diag) catch |err| { + // Report useful error and exit + diag.report(std.io.getStdErr().outStream(), err) catch {}; + return err; + }; + defer args.deinit(); + + if (args.flag("--help")) + debug.warn("--help\n", .{}); + if (args.option("--number")) |n| + debug.warn("--number = {}\n", .{n}); + for (args.options("--string")) |s| + debug.warn("--string = {}\n", .{s}); + for (args.positionals()) |pos| + debug.warn("{}\n", .{pos}); +} -- cgit v1.2.3