From 5166a15378a9c32d9b680417807000ba65d06141 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 23 Mar 2022 20:05:28 +0100 Subject: Add parseParams and friends --- example/README.md.template | 4 ++-- example/help.zig | 9 +++++---- example/simple-ex.zig | 15 ++++++++------- example/simple.zig | 15 ++++++++------- example/usage.zig | 11 ++++++----- 5 files changed, 29 insertions(+), 25 deletions(-) (limited to 'example') diff --git a/example/README.md.template b/example/README.md.template index 9fbc1cc..c19c1cd 100644 --- a/example/README.md.template +++ b/example/README.md.template @@ -35,8 +35,8 @@ The fields in `args` are typed. The type is based on the name of the value the p Since `--number` takes a `usize` the field `res.args.number` has the type `usize`. Note that this is only the case because `clap.parsers.default` has a field called `usize` which -contains a parser that returns `usize`. You can pass in something other than `clap.parsers.default` -if you want some other mapping. +contains a parser that returns `usize`. You can pass in something other than +`clap.parsers.default` if you want some other mapping. ```zig {s} diff --git a/example/help.zig b/example/help.zig index f3edb58..64d1709 100644 --- a/example/help.zig +++ b/example/help.zig @@ -2,10 +2,11 @@ const clap = @import("clap"); 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, - }; + const params = comptime clap.parseParamsComptime( + \\-h, --help Display this help and exit. + \\-v, --version Output version information and exit. + \\ + ); var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); defer res.deinit(); diff --git a/example/simple-ex.zig b/example/simple-ex.zig index 6cb4c3f..d0d214d 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig @@ -7,13 +7,14 @@ const process = std.process; 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, - }; + // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` + const params = comptime clap.parseParamsComptime( + \\-h, --help Display this help and exit. + \\-n, --number An option parameter, which takes a value. + \\-s, --string ... An option parameter which can be specified multiple times. + \\... + \\ + ); // Declare our own parsers which are used to map the argument strings to other // types. diff --git a/example/simple.zig b/example/simple.zig index 11e975e..1ac7de5 100644 --- a/example/simple.zig +++ b/example/simple.zig @@ -6,13 +6,14 @@ const io = std.io; 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, - }; + // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` + const params = comptime clap.parseParamsComptime( + \\-h, --help Display this help and exit. + \\-n, --number An option parameter, which takes a value. + \\-s, --string ... An option parameter which can be specified multiple times. + \\... + \\ + ); // 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 diff --git a/example/usage.zig b/example/usage.zig index 20d4736..f57b07c 100644 --- a/example/usage.zig +++ b/example/usage.zig @@ -2,11 +2,12 @@ const clap = @import("clap"); 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, - }; + const params = comptime clap.parseParamsComptime( + \\-h, --help Display this help and exit. + \\-v, --version Output version information and exit. + \\ --value An option parameter, which takes a value. + \\ + ); var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{}); defer res.deinit(); -- cgit v1.2.3