From a305e818bd986c599fff17141617bc4f890276cf Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 27 Nov 2019 23:13:31 +0900 Subject: Add clap.parse as the simplest way of using the lib --- example/simple.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 example/simple.zig (limited to 'example/simple.zig') diff --git a/example/simple.zig b/example/simple.zig new file mode 100644 index 0000000..f791447 --- /dev/null +++ b/example/simple.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const clap = @import("clap"); + +const debug = std.debug; + +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.Param(clap.Help){ + .takes_value = true, + }, + }; + + var args = try clap.parse(clap.Help, params, std.heap.direct_allocator); + defer args.deinit(); + + if (args.flag("--help")) + debug.warn("--help\n"); + if (args.option("--number")) |n| + debug.warn("--number = {}\n", n); + for (args.positionals()) |pos| + debug.warn("{}\n", pos); +} -- cgit v1.2.3