summaryrefslogtreecommitdiff
path: root/src/index.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.zig')
-rw-r--r--src/index.zig30
1 files changed, 14 insertions, 16 deletions
diff --git a/src/index.zig b/src/index.zig
index e85470c..40ad849 100644
--- a/src/index.zig
+++ b/src/index.zig
@@ -41,10 +41,10 @@ pub const Names = struct {
41 }; 41 };
42 } 42 }
43 43
44 /// Initializes a name with a prefix. 44 /// Initializes a name that is long and short, from the same string.
45 /// ::short is set to ::name[0], and ::long is set to ::name. 45 /// ::short is set to ::name[0], and ::long is set to ::name.
46 /// This function asserts that ::name.len != 0 46 /// This function asserts that ::name.len != 0
47 pub fn prefix(name: []const u8) Names { 47 pub fn both(name: []const u8) Names {
48 debug.assert(name.len != 0); 48 debug.assert(name.len != 0);
49 49
50 return Names{ 50 return Names{
@@ -119,14 +119,13 @@ pub fn helpFull(
119 params: []const Param(Id), 119 params: []const Param(Id),
120 comptime Error: type, 120 comptime Error: type,
121 context: var, 121 context: var,
122 help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 122 help_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
123 value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 123 value_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
124) !void { 124) !void {
125 const max_spacing = blk: { 125 const max_spacing = blk: {
126 var null_stream = io.NullOutStream.init();
127 var res: usize = 0; 126 var res: usize = 0;
128 for (params) |param| { 127 for (params) |param| {
129 var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); 128 var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(io.null_out_stream);
130 try printParam(&counting_stream.stream, Id, param, Error, context, value_text); 129 try printParam(&counting_stream.stream, Id, param, Error, context, value_text);
131 if (res < counting_stream.bytes_written) 130 if (res < counting_stream.bytes_written)
132 res = counting_stream.bytes_written; 131 res = counting_stream.bytes_written;
@@ -153,7 +152,7 @@ fn printParam(
153 param: Param(Id), 152 param: Param(Id),
154 comptime Error: type, 153 comptime Error: type,
155 context: var, 154 context: var,
156 value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 155 value_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
157) @typeOf(stream.*).Error!void { 156) @typeOf(stream.*).Error!void {
158 if (param.names.short) |s| { 157 if (param.names.short) |s| {
159 try stream.print("-{c}", s); 158 try stream.print("-{c}", s);
@@ -179,12 +178,12 @@ pub fn helpEx(
179 stream: var, 178 stream: var,
180 comptime Id: type, 179 comptime Id: type,
181 params: []const Param(Id), 180 params: []const Param(Id),
182 help_text: fn(Param(Id)) []const u8, 181 help_text: fn (Param(Id)) []const u8,
183 value_text: fn(Param(Id)) []const u8, 182 value_text: fn (Param(Id)) []const u8,
184) !void { 183) !void {
185 const Context = struct { 184 const Context = struct {
186 help_text: fn(Param(Id)) []const u8, 185 help_text: fn (Param(Id)) []const u8,
187 value_text: fn(Param(Id)) []const u8, 186 value_text: fn (Param(Id)) []const u8,
188 187
189 pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { 188 pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 {
190 return c.help_text(p); 189 return c.help_text(p);
@@ -223,7 +222,6 @@ fn getValueSimple(param: Param([]const u8)) []const u8 {
223 return "VALUE"; 222 return "VALUE";
224} 223}
225 224
226
227test "clap.help" { 225test "clap.help" {
228 var buf: [1024]u8 = undefined; 226 var buf: [1024]u8 = undefined;
229 var slice_stream = io.SliceOutStream.init(buf[0..]); 227 var slice_stream = io.SliceOutStream.init(buf[0..]);
@@ -248,19 +246,19 @@ test "clap.help" {
248 ), 246 ),
249 Param([]const u8).flag( 247 Param([]const u8).flag(
250 "Both flag.", 248 "Both flag.",
251 Names.prefix("cc"), 249 Names.both("cc"),
252 ), 250 ),
253 Param([]const u8).option( 251 Param([]const u8).option(
254 "Both option.", 252 "Both option.",
255 Names.prefix("dd"), 253 Names.both("dd"),
256 ), 254 ),
257 Param([]const u8).positional( 255 Param([]const u8).positional(
258 "Positional. This should not appear in the help message." 256 "Positional. This should not appear in the help message.",
259 ), 257 ),
260 }, 258 },
261 ); 259 );
262 260
263 const expected = 261 const expected = "" ++
264 "\t-a \tShort flag.\n" ++ 262 "\t-a \tShort flag.\n" ++
265 "\t-b=VALUE \tShort option.\n" ++ 263 "\t-b=VALUE \tShort option.\n" ++
266 "\t --aa \tLong flag.\n" ++ 264 "\t --aa \tLong flag.\n" ++