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 --- src/index.zig | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) (limited to 'src') diff --git a/src/index.zig b/src/index.zig index 225eb9c..0914176 100644 --- a/src/index.zig +++ b/src/index.zig @@ -1,6 +1,8 @@ const std = @import("std"); const debug = std.debug; +const io = std.io; +const mem = std.mem; pub const @"comptime" = @import("comptime.zig"); pub const args = @import("args.zig"); @@ -106,3 +108,135 @@ pub fn Param(comptime Id: type) type { } }; } + +/// Will print a help message in the following format: +/// -s, --long=value_text help_text +/// -s, help_text +/// --long help_text +pub fn helpEx( + stream: var, + comptime Id: type, + params: []const Param(Id), + comptime Error: type, + context: var, + help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, + value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, +) !void { + const max_spacing = blk: { + var null_stream = io.NullOutStream.init(); + var res: usize = 0; + for (params) |param| { + var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); + try printParam(&counting_stream.stream, Id, param, Error, context, value_text); + if (res < counting_stream.bytes_written) + res = counting_stream.bytes_written; + } + + break :blk res; + }; + + for (params) |param| { + if (param.names.short == null and param.names.long == null) + continue; + + var counting_stream = io.CountingOutStream(@typeOf(stream.*).Error).init(stream); + try stream.print("\t"); + try printParam(&counting_stream.stream, Id, param, Error, context, value_text); + try stream.writeByteNTimes(' ', max_spacing - counting_stream.bytes_written); + try stream.print("\t{}\n", try help_text(context, param)); + } +} + +fn printParam( + stream: var, + comptime Id: type, + param: Param(Id), + comptime Error: type, + context: var, + value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, +) @typeOf(stream.*).Error!void { + if (param.names.short) |s| { + try stream.print("-{c}", s); + } else { + try stream.print(" "); + } + if (param.names.long) |l| { + if (param.names.short) |_| { + try stream.print(", "); + } else { + try stream.print(" "); + } + + try stream.print("--{}", l); + } + if (param.takes_value) + try stream.print("={}", value_text(context, param)); +} + +/// 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); +} + +fn getHelpSimple(context: void, param: Param([]const u8)) error{}![]const u8 { + return param.id; +} + +fn getValueSimple(context: void, param: Param([]const u8)) error{}![]const u8 { + return "VALUE"; +} + + +test "clap.help" { + var buf: [1024]u8 = undefined; + var slice_stream = io.SliceOutStream.init(buf[0..]); + try help( + &slice_stream.stream, + []Param([]const u8){ + Param([]const u8).flag( + "Short flag.", + Names.short('a'), + ), + Param([]const u8).option( + "Short option.", + Names.short('b'), + ), + Param([]const u8).flag( + "Long flag.", + Names.long("aa"), + ), + Param([]const u8).option( + "Long option.", + Names.long("bb"), + ), + Param([]const u8).flag( + "Both flag.", + Names.prefix("cc"), + ), + Param([]const u8).option( + "Both option.", + Names.prefix("dd"), + ), + Param([]const u8).positional( + "Positional. This should not appear in the help message." + ), + }, + ); + + const expected = + "\t-a \tShort flag.\n" ++ + "\t-b=VALUE \tShort option.\n" ++ + "\t --aa \tLong flag.\n" ++ + "\t --bb=VALUE\tLong option.\n" ++ + "\t-c, --cc \tBoth flag.\n" ++ + "\t-d, --dd=VALUE\tBoth option.\n"; + + if (!mem.eql(u8, slice_stream.getWritten(), expected)) { + debug.warn("============ Expected ============\n"); + debug.warn("{}", expected); + debug.warn("============= Actual =============\n"); + debug.warn("{}", slice_stream.getWritten()); + return error.NoMatch; + } +} -- cgit v1.2.3