From 5f7b75d5523d9581eca5a72a362868ff517992e8 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 25 Feb 2022 19:40:00 +0100 Subject: Allow for clap to parse argument values into types This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access on the result of `parse` and `parseEx`. - `clap.ComptimeClap` has been removed. - `clap.StreamingClap` is now called `clap.streaming.Clap` - `parse` and `parseEx` now takes a `value_parsers` argument that provides the parsers to parse values. - Remove `helpEx`, `helpFull`, `usageEx` and `usageFull`. They now just expect `Id` to have methods for getting the description and value texts. --- example/simple-ex.zig | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'example/simple-ex.zig') diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d2dc77e..6cb4c3f 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -6,43 +6,38 @@ const io = std.io; const process = std.process; 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("-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, + clap.parseParam("...") catch unreachable, }; - var iter = try process.ArgIterator.initWithAllocator(allocator); - defer iter.deinit(); - - // Skip exe argument - _ = iter.next(); + // Declare our own parsers which are used to map the argument strings to other + // types. + const parsers = comptime .{ + .STR = clap.parsers.string, + .FILE = clap.parsers.string, + .INT = clap.parsers.int(usize, 10), + }; - // Initalize our diagnostics, which can be used for reporting useful errors. - // This is optional. You can also pass `.{}` to `clap.parse` if you don't - // care about the extra information `Diagnostics` provides. var diag = clap.Diagnostic{}; - var args = clap.parseEx(clap.Help, ¶ms, &iter, .{ - .allocator = allocator, + var res = clap.parse(clap.Help, ¶ms, parsers, .{ .diagnostic = &diag, }) catch |err| { - // Report useful error and exit diag.report(io.getStdErr().writer(), err) catch {}; return err; }; - defer args.deinit(); + defer res.deinit(); - if (args.flag("--help")) + if (res.args.help) debug.print("--help\n", .{}); - if (args.option("--number")) |n| - debug.print("--number = {s}\n", .{n}); - for (args.options("--string")) |s| + if (res.args.number) |n| + debug.print("--number = {}\n", .{n}); + for (res.args.string) |s| debug.print("--string = {s}\n", .{s}); - for (args.positionals()) |pos| + for (res.positionals) |pos| debug.print("{s}\n", .{pos}); } -- cgit v1.2.3