From c564b168785e740c37f47da4942825b25cb8b4ec Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 14 Nov 2018 14:06:20 +0100 Subject: Restructured and make StreamingClap simpler * Also added a ComptimeClap --- example/comptime-clap.zig | 47 ++++++++++++++++++++++++++++++++++++++++ example/streaming-clap.zig | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 example/comptime-clap.zig create mode 100644 example/streaming-clap.zig (limited to 'example') diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig new file mode 100644 index 0000000..e44d9b1 --- /dev/null +++ b/example/comptime-clap.zig @@ -0,0 +1,47 @@ +const std = @import("std"); +const clap = @import("clap"); + +const debug = std.debug; + +pub fn main() !void { + var direct_allocator = std.heap.DirectAllocator.init(); + const allocator = &direct_allocator.allocator; + defer direct_allocator.deinit(); + + // First we specify what parameters our program can take. + const params = comptime []clap.Param(void){ + // Param.init takes 3 arguments. + // * An "id", which can be any type specified by the argument to Param. The + // ComptimeClap expects clap.Param(void) only. + // * A bool which determins wether the parameter takes a value. + // * A "Names" struct, which determins what names the parameter will have on the + // commandline. Names.prefix inits a "Names" struct that has the "short" name + // set to the first letter, and the "long" name set to the full name. + clap.Param(void).init({}, false, clap.Names.prefix("help")), + clap.Param(void).init({}, true, clap.Names.prefix("number")), + + // Names.positional returns a "Names" struct where neither the "short" or "long" + // name is set. + clap.Param(void).init({}, true, clap.Names.positional()), + }; + + // We then initialize an argument iterator. We will use the OsIterator as it nicely + // wraps iterating over arguments the most efficient way on each os. + var os_iter = clap.args.OsIterator.init(allocator); + const iter = &os_iter.iter; + defer os_iter.deinit(); + + // Consume the exe arg. + const exe = try iter.next(); + + // Finally we can parse the arguments + var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator.Error, iter); + 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); +} diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig new file mode 100644 index 0000000..0dc2bf7 --- /dev/null +++ b/example/streaming-clap.zig @@ -0,0 +1,54 @@ +const std = @import("std"); +const clap = @import("clap"); + +const debug = std.debug; + +pub fn main() !void { + var direct_allocator = std.heap.DirectAllocator.init(); + const allocator = &direct_allocator.allocator; + defer direct_allocator.deinit(); + + // First we specify what parameters our program can take. + const params = []clap.Param(u8){ + // Param.init takes 3 arguments. + // * An "id", which can be any type specified by the argument to Param. Here, we + // use a "u8" as the "id" type. + // * A bool which determins wether the parameter takes a value. + // * A "Names" struct, which determins what names the parameter will have on the + // commandline. Names.prefix inits a "Names" struct that has the "short" name + // set to the first letter, and the "long" name set to the full name. + clap.Param(u8).init('h', false, clap.Names.prefix("help")), + clap.Param(u8).init('n', true, clap.Names.prefix("number")), + + // Names.positional returns a "Names" struct where neither the "short" or "long" + // name is set. + clap.Param(u8).init('f', true, clap.Names.positional()), + }; + + // We then initialize an argument iterator. We will use the OsIterator as it nicely + // wraps iterating over arguments the most efficient way on each os. + var os_iter = clap.args.OsIterator.init(allocator); + const iter = &os_iter.iter; + defer os_iter.deinit(); + + // Consume the exe arg. + const exe = try iter.next(); + + // Finally we initialize our streaming parser. + var parser = clap.StreamingClap(u8, clap.args.OsIterator.Error).init(params, iter); + + // Because we use a streaming parser, we have to consume each argument parsed individually. + while (try parser.next()) |arg| { + // 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.?), + + // 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.?), + else => unreachable, + } + } +} -- cgit v1.2.3