From 988375ab791178a472ecff604aba2167b1cae87a Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 15 Jun 2023 09:53:14 +0200 Subject: help,usage: Support multiple positionals (#97) --- clap.zig | 59 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/clap.zig b/clap.zig index 8a9b961..27647e6 100644 --- a/clap.zig +++ b/clap.zig @@ -1142,8 +1142,6 @@ pub fn help( var first_paramter: bool = true; for (params) |param| { - if (param.names.longest().kind == .positional) - continue; if (!first_paramter) try writer.writeByteNTimes('\n', opt.spacing_between_parameters); @@ -1308,21 +1306,26 @@ fn printParam( comptime Id: type, param: Param(Id), ) !void { - try stream.writeAll(&[_]u8{ - if (param.names.short) |_| '-' else ' ', - param.names.short orelse ' ', - }); + if (param.names.short != null or param.names.long != null) { + try stream.writeAll(&[_]u8{ + if (param.names.short) |_| '-' else ' ', + param.names.short orelse ' ', + }); + + if (param.names.long) |l| { + try stream.writeByte(if (param.names.short) |_| ',' else ' '); + try stream.writeAll(" --"); + try stream.writeAll(l); + } - if (param.names.long) |l| { - try stream.writeByte(if (param.names.short) |_| ',' else ' '); - try stream.writeAll(" --"); - try stream.writeAll(l); + if (param.takes_value != .none) + try stream.writeAll(" "); } if (param.takes_value == .none) return; - try stream.writeAll(" <"); + try stream.writeAll("<"); try stream.writeAll(param.id.value()); try stream.writeAll(">"); if (param.takes_value == .many) @@ -1372,6 +1375,12 @@ test "clap.help" { \\ -d, --dd ... \\ Both repeated option. \\ + \\ + \\ Help text + \\ + \\ ... + \\ Another help text + \\ ); try testHelp(.{ .markdown_lite = false }, @@ -1731,7 +1740,7 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi if (cos.bytes_written != 0) try cs.writeAll("]"); - var positional: ?Param(Id) = null; + var has_positionals: bool = false; for (params) |param| { if (param.takes_value == .none and param.names.short != null) continue; @@ -1743,7 +1752,7 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi @ptrCast([*]const u8, s)[0..1] else param.names.long orelse { - positional = param; + has_positionals = true; continue; }; @@ -1764,14 +1773,20 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi try cs.writeByte(']'); } - if (positional) |p| { + if (!has_positionals) + return; + + for (params) |param| { + if (param.names.short != null or param.names.long != null) + continue; + if (cos.bytes_written != 0) try cs.writeAll(" "); try cs.writeAll("<"); - try cs.writeAll(p.id.value()); + try cs.writeAll(param.id.value()); try cs.writeAll(">"); - if (p.takes_value == .many) + if (param.takes_value == .many) try cs.writeAll("..."); } } @@ -1829,4 +1844,16 @@ test "usage" { \\ ), ); + try testUsage(" ", &comptime parseParamsComptime( + \\ + \\ + \\ + \\ + )); + try testUsage(" ...", &comptime parseParamsComptime( + \\ + \\ + \\... + \\ + )); } -- cgit v1.2.3