From a3d2a261f59983838e3ed5f01d90f18352e6a421 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Fri, 28 Aug 2020 17:26:01 +1000 Subject: adjust examples, README template --- README.md | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 2b38281..2037b04 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ into master on every `zig` release. * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) * Short args also support passing values with no spacing or `=` (`-a100`) * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`) +* Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`) * Print help message from parameter specification. * Parse help message to parameter specification. @@ -33,10 +34,11 @@ 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("-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.Param(clap.Help){ - .takes_value = true, + .takes_value = .One, }, }; @@ -46,15 +48,18 @@ pub fn main() !void { if (args.flag("--help")) debug.warn("--help\n", .{}); if (args.option("--number")) |n| - debug.warn("--number = {}\n", .{ n }); + debug.warn("--number = {}\n", .{n}); + for (args.options("--string")) |s| + debug.warn("--string = {}\n", .{s}); for (args.positionals()) |pos| - debug.warn("{}\n", .{ pos }); + debug.warn("{}\n", .{pos}); } ``` The data structure returned has lookup speed on par with array access (`arr[i]`) and validates -that the strings you pass to `option` and `flag` are actually parameters that the program can take: +that the strings you pass to `option`, `options` and `flag` are actually parameters that the +program can take: ```zig const std = @import("std"); @@ -106,10 +111,11 @@ 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("-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.Param(clap.Help){ - .takes_value = true, + .takes_value = .One, }, }; @@ -125,9 +131,11 @@ pub fn main() !void { if (args.flag("--help")) debug.warn("--help\n", .{}); if (args.option("--number")) |n| - debug.warn("--number = {}\n", .{ n }); + debug.warn("--number = {}\n", .{n}); + for (args.options("--string")) |s| + debug.warn("--string = {}\n", .{s}); for (args.positionals()) |pos| - debug.warn("{}\n", .{ pos }); + debug.warn("{}\n", .{pos}); } ``` @@ -155,11 +163,11 @@ pub fn main() !void { clap.Param(u8){ .id = 'n', .names = clap.Names{ .short = 'n', .long = "number" }, - .takes_value = true, + .takes_value = .One, }, clap.Param(u8){ .id = 'f', - .takes_value = true, + .takes_value = .One, }, }; @@ -179,12 +187,12 @@ pub fn main() !void { // arg.param will point to the parameter which matched the argument. switch (arg.param.id) { 'h' => debug.warn("Help!\n", .{}), - 'n' => debug.warn("--number = {}\n", .{ arg.value.? }), + 'n' => debug.warn("--number = {}\n", .{arg.value.?}), // arg.value == null, if arg.param.takes_value == false. // Otherwise, arg.value is the value passed with the argument, such as "-a=10" // or "-a 10". - 'f' => debug.warn("{}\n", .{ arg.value.? }), + 'f' => debug.warn("{}\n", .{arg.value.?}), else => unreachable, } } -- cgit v1.2.3