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 --- src/index.zig | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/index.zig') 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