From 1a219fc680faa79f1236912b14fd58754313df87 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Thu, 15 Nov 2018 12:53:46 +0100 Subject: Added help function --- example/comptime-clap.zig | 35 +++++++++++++++++++---------------- example/streaming-clap.zig | 10 ---------- 2 files changed, 19 insertions(+), 26 deletions(-) (limited to 'example') diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig index 3b7b42b..b275dc7 100644 --- a/example/comptime-clap.zig +++ b/example/comptime-clap.zig @@ -4,25 +4,25 @@ const clap = @import("clap"); const debug = std.debug; pub fn main() !void { + const stdout_file = try std.io.getStdOut(); + var stdout_out_stream = stdout_file.outStream(); + const stdout = &stdout_out_stream.stream; + 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).flag({}, clap.Names.prefix("help")), - clap.Param(void).option({}, clap.Names.prefix("number")), - - // Names.positional returns a "Names" struct where neither the "short" or "long" - // name is set. - clap.Param(void).positional({}), + const params = comptime []clap.Param([]const u8){ + clap.Param([]const u8).flag( + "Display this help and exit.", + clap.Names.prefix("help") + ), + clap.Param([]const u8).option( + "An option parameter, which takes a value.", + clap.Names.prefix("number"), + ), + clap.Param([]const u8).positional(""), }; // We then initialize an argument iterator. We will use the OsIterator as it nicely @@ -35,11 +35,14 @@ pub fn main() !void { 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); + var args = try clap.ComptimeClap([]const u8, params).parse(allocator, clap.args.OsIterator.Error, iter); defer args.deinit(); + // clap.help is a function that can print a simple help message, given a + // slice of Param([]const u8). There is also a helpEx, which can print a + // help message for any Param, but it is more verbose to call. if (args.flag("--help")) - debug.warn("Help!\n"); + return try clap.help(stdout, params); if (args.option("--number")) |n| debug.warn("--number = {}\n", n); for (args.positionals()) |pos| diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 013c1d4..57ebe71 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig @@ -10,18 +10,8 @@ pub fn main() !void { // 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).flag('h', clap.Names.prefix("help")), clap.Param(u8).option('n', clap.Names.prefix("number")), - - // Names.positional returns a "Names" struct where neither the "short" or "long" - // name is set. clap.Param(u8).positional('f'), }; -- cgit v1.2.3