From b2059e5d086731a37f5988bfc6bfdb0848f14ece Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Mon, 29 Nov 2021 17:01:36 +0100 Subject: Improve help and usage examples Instead of just calling these function, have the examples be small programs that demonstrates how you would actually use them together with argument parsing. fixes #57 --- README.md | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 197eb4c..d6ad7a0 100644 --- a/README.md +++ b/README.md @@ -180,21 +180,25 @@ 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, + }; + + var args = try clap.parse(clap.Help, ¶ms, .{}); + defer args.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. - try clap.help( - std.io.getStdErr().writer(), - comptime &.{ - clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, - clap.parseParam("-v, --version Output version information and exit.") catch unreachable, - }, - ); + if (args.flag("--help")) + return clap.help(std.io.getStdErr().writer(), ¶ms); } ``` ``` +$ zig-out/bin/help --help -h, --help Display this help and exit. -v, --version Output version information and exit. ``` @@ -218,22 +222,26 @@ 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, + }; + + var args = try clap.parse(clap.Help, ¶ms, .{}); + defer args.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. - try clap.usage( - std.io.getStdErr().writer(), - comptime &.{ - 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, - }, - ); + if (args.flag("--help")) + return clap.usage(std.io.getStdErr().writer(), ¶ms); } ``` ``` +$ zig-out/bin/usage --help [-hv] [--value ] ``` -- cgit v1.2.3