From e34633492fc702c2f32b1ad46edbbc69e6ec4034 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Thu, 15 Nov 2018 15:41:19 +0100 Subject: Renamed helpEx to helpFull and added a new helpEx that wraps helpFull --- README.md | 42 ++++-------------------------------------- src/index.zig | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index e9d1709..2bc86a6 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ Ofc, this limits you to use only parameters that are comptime known. ### `help` -The `help` and `helpEx` are functions for printing a simple list of all parameters the +The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the program can take. ```rust @@ -142,41 +142,7 @@ The `help` function is the simplest to call. It only takes an `OutStream` and a `Param([]const u8)`. This function assumes that the id of each parameter is the help message. The `helpEx` is the generic version of `help`. It can print a help message for any -`Param`, but requires some extra arguments to work. +`Param` give that the caller provides functions for getting the help and value strings. -```rust -fn getHelp(_: void, param: clap.Param(u8)) error{}![]const u8 { - return switch (param.id) { - 'h' => "Display this help and exit.", - 'v' => "Output version information and exit.", - else => unreachable, - }; -} - -fn getValue(_: void, param: clap.Param(u8)) error{}![]const u8 { - return ""; -} - -const stderr_file = try std.io.getStdErr(); -var stderr_out_stream = stderr_file.outStream(); -const stderr = &stderr_out_stream.stream; - -try stderr.print("\n"); -try clap.helpEx( - stderr, - u8, - []clap.Param(u8){ - clap.Param(u8).flag('h', clap.Names.prefix("help")), - clap.Param(u8).flag('v', clap.Names.prefix("version")), - }, - error{}, - {}, - getHelp, - getValue, -); -``` - -``` - -h, --help Display this help and exit. - -v, --version Output version information and exit. -``` +The `helpFull` is even more generic, allowing the functions that get the help and value strings +to return errors and take a context as a parameter. diff --git a/src/index.zig b/src/index.zig index 0914176..e85470c 100644 --- a/src/index.zig +++ b/src/index.zig @@ -113,7 +113,7 @@ pub fn Param(comptime Id: type) type { /// -s, --long=value_text help_text /// -s, help_text /// --long help_text -pub fn helpEx( +pub fn helpFull( stream: var, comptime Id: type, params: []const Param(Id), @@ -173,17 +173,53 @@ fn printParam( try stream.print("={}", value_text(context, param)); } +/// A wrapper around helpFull for simple help_text and value_text functions that +/// cant return an error or take a context. +pub fn helpEx( + stream: var, + comptime Id: type, + params: []const Param(Id), + help_text: fn(Param(Id)) []const u8, + value_text: fn(Param(Id)) []const u8, +) !void { + const Context = struct { + help_text: fn(Param(Id)) []const u8, + value_text: fn(Param(Id)) []const u8, + + pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { + return c.help_text(p); + } + + pub fn value(c: @This(), p: Param(Id)) error{}![]const u8 { + return c.value_text(p); + } + }; + + return helpFull( + stream, + Id, + params, + error{}, + Context{ + .help_text = help_text, + .value_text = value_text, + }, + Context.help, + Context.value, + ); +} + /// A wrapper around helpEx that takes a Param([]const u8) and uses the string id /// as the help text for each paramter. pub fn help(stream: var, params: []const Param([]const u8)) !void { - try helpEx(stream, []const u8, params, error{}, {}, getHelpSimple, getValueSimple); + try helpEx(stream, []const u8, params, getHelpSimple, getValueSimple); } -fn getHelpSimple(context: void, param: Param([]const u8)) error{}![]const u8 { +fn getHelpSimple(param: Param([]const u8)) []const u8 { return param.id; } -fn getValueSimple(context: void, param: Param([]const u8)) error{}![]const u8 { +fn getValueSimple(param: Param([]const u8)) []const u8 { return "VALUE"; } -- cgit v1.2.3