From cfaac64c404fb1c2e892880410aa3b7dd881ea58 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 25 Feb 2022 19:40:00 +0100 Subject: Change clap into generating a struct This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access. - Move the current `clap.parse` and friends into `clap.untyped.parse` - This is in preperation for `clap.typed.parse` --- example/README.md.template | 4 ++-- example/help.zig | 10 +++++----- example/simple-ex.zig | 20 ++++++++++---------- example/simple.zig | 20 ++++++++++---------- example/streaming-clap.zig | 2 +- example/usage.zig | 12 ++++++------ 6 files changed, 34 insertions(+), 34 deletions(-) (limited to 'example') diff --git a/example/README.md.template b/example/README.md.template index 7f5c545..d792152 100644 --- a/example/README.md.template +++ b/example/README.md.template @@ -51,9 +51,9 @@ zig-clap/example/simple-error.zig:16:18: note: called from here There is also a `parseEx` variant that takes an argument iterator. -### `StreamingClap` +### `streaming.Clap` -The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an +The `streaming.Clap` is the base of all the other parsers. It's a streaming parser that uses an `args.Iterator` to provide it with arguments lazily. ```zig diff --git a/example/help.zig b/example/help.zig index de3b707..2e7ca2b 100644 --- a/example/help.zig +++ b/example/help.zig @@ -3,16 +3,16 @@ const std = @import("std"); pub fn main() !void { const params = comptime [_]clap.Param(clap.Help){ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-v, --version Output version information and exit.") catch unreachable, + clap.untyped.parseParam("-h, --help Display this help and exit. ") catch unreachable, + clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable, }; - var args = try clap.parse(clap.Help, ¶ms, .{}); - defer args.deinit(); + var res = try clap.untyped.parse(clap.Help, ¶ms, .{}); + defer res.deinit(); // clap.help is a function that can print a simple help message, given a // slice of Param(Help). There is also a helpEx, which can print a // help message for any Param, but it is more verbose to call. - if (args.flag("--help")) + if (res.args.help) return clap.help(std.io.getStdErr().writer(), ¶ms); } diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d2dc77e..f1a958d 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -11,10 +11,10 @@ pub fn main() !void { // 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, + clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable, + clap.untyped.parseParam("-n, --number An option parameter, which takes a value.") catch unreachable, + clap.untyped.parseParam("-s, --string ... An option parameter which can be specified multiple times.") catch unreachable, + clap.untyped.parseParam("...") catch unreachable, }; var iter = try process.ArgIterator.initWithAllocator(allocator); @@ -27,7 +27,7 @@ pub fn main() !void { // 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, .{ + var res = clap.untyped.parseEx(clap.Help, ¶ms, &iter, .{ .allocator = allocator, .diagnostic = &diag, }) catch |err| { @@ -35,14 +35,14 @@ pub fn main() !void { 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| + if (res.args.number) |n| debug.print("--number = {s}\n", .{n}); - for (args.options("--string")) |s| + for (res.args.string) |s| debug.print("--string = {s}\n", .{s}); - for (args.positionals()) |pos| + for (res.positionals) |pos| debug.print("{s}\n", .{pos}); } diff --git a/example/simple.zig b/example/simple.zig index ff6d301..c37e896 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -8,29 +8,29 @@ pub fn main() !void { // 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, + clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable, + clap.untyped.parseParam("-n, --number An option parameter, which takes a value.") catch unreachable, + clap.untyped.parseParam("-s, --string ... An option parameter which can be specified multiple times.") catch unreachable, + clap.untyped.parseParam("...") catch unreachable, }; // 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.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { + var res = clap.untyped.parse(clap.Help, ¶ms, .{ .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| + if (res.args.number) |n| debug.print("--number = {s}\n", .{n}); - for (args.options("--string")) |s| + for (res.args.string) |s| debug.print("--string = {s}\n", .{s}); - for (args.positionals()) |pos| + for (res.positionals) |pos| debug.print("{s}\n", .{pos}); } diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index a7ab7d8..cacda56 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -32,7 +32,7 @@ pub fn main() !void { // This is optional. You can also leave the `diagnostic` field unset if you // don't care about the extra information `Diagnostic` provides. var diag = clap.Diagnostic{}; - var parser = clap.StreamingClap(u8, process.ArgIterator){ + var parser = clap.streaming.Clap(u8, process.ArgIterator){ .params = ¶ms, .iter = &iter, .diagnostic = &diag, diff --git a/example/usage.zig b/example/usage.zig index 368a6b3..04fedba 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -3,17 +3,17 @@ const std = @import("std"); pub fn main() !void { const params = comptime [_]clap.Param(clap.Help){ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-v, --version Output version information and exit. ") catch unreachable, - clap.parseParam(" --value An option parameter, which takes a value.") catch unreachable, + clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable, + clap.untyped.parseParam("-v, --version Output version information and exit.") catch unreachable, + clap.untyped.parseParam(" --value An option parameter, which takes a value.") catch unreachable, }; - var args = try clap.parse(clap.Help, ¶ms, .{}); - defer args.deinit(); + var res = try clap.untyped.parse(clap.Help, ¶ms, .{}); + defer res.deinit(); // clap.usage is a function that can print a simple usage message, given a // slice of Param(Help). There is also a usageEx, which can print a // usage message for any Param, but it is more verbose to call. - if (args.flag("--help")) + if (res.args.help) return clap.usage(std.io.getStdErr().writer(), ¶ms); } -- cgit v1.2.3