summaryrefslogtreecommitdiff
path: root/clap.zig
diff options
context:
space:
mode:
Diffstat (limited to 'clap.zig')
-rw-r--r--clap.zig59
1 files 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(
1142 1142
1143 var first_paramter: bool = true; 1143 var first_paramter: bool = true;
1144 for (params) |param| { 1144 for (params) |param| {
1145 if (param.names.longest().kind == .positional)
1146 continue;
1147 if (!first_paramter) 1145 if (!first_paramter)
1148 try writer.writeByteNTimes('\n', opt.spacing_between_parameters); 1146 try writer.writeByteNTimes('\n', opt.spacing_between_parameters);
1149 1147
@@ -1308,21 +1306,26 @@ fn printParam(
1308 comptime Id: type, 1306 comptime Id: type,
1309 param: Param(Id), 1307 param: Param(Id),
1310) !void { 1308) !void {
1311 try stream.writeAll(&[_]u8{ 1309 if (param.names.short != null or param.names.long != null) {
1312 if (param.names.short) |_| '-' else ' ', 1310 try stream.writeAll(&[_]u8{
1313 param.names.short orelse ' ', 1311 if (param.names.short) |_| '-' else ' ',
1314 }); 1312 param.names.short orelse ' ',
1313 });
1314
1315 if (param.names.long) |l| {
1316 try stream.writeByte(if (param.names.short) |_| ',' else ' ');
1317 try stream.writeAll(" --");
1318 try stream.writeAll(l);
1319 }
1315 1320
1316 if (param.names.long) |l| { 1321 if (param.takes_value != .none)
1317 try stream.writeByte(if (param.names.short) |_| ',' else ' '); 1322 try stream.writeAll(" ");
1318 try stream.writeAll(" --");
1319 try stream.writeAll(l);
1320 } 1323 }
1321 1324
1322 if (param.takes_value == .none) 1325 if (param.takes_value == .none)
1323 return; 1326 return;
1324 1327
1325 try stream.writeAll(" <"); 1328 try stream.writeAll("<");
1326 try stream.writeAll(param.id.value()); 1329 try stream.writeAll(param.id.value());
1327 try stream.writeAll(">"); 1330 try stream.writeAll(">");
1328 if (param.takes_value == .many) 1331 if (param.takes_value == .many)
@@ -1372,6 +1375,12 @@ test "clap.help" {
1372 \\ -d, --dd <V3>... 1375 \\ -d, --dd <V3>...
1373 \\ Both repeated option. 1376 \\ Both repeated option.
1374 \\ 1377 \\
1378 \\ <A>
1379 \\ Help text
1380 \\
1381 \\ <B>...
1382 \\ Another help text
1383 \\
1375 ); 1384 );
1376 1385
1377 try testHelp(.{ .markdown_lite = false }, 1386 try testHelp(.{ .markdown_lite = false },
@@ -1731,7 +1740,7 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi
1731 if (cos.bytes_written != 0) 1740 if (cos.bytes_written != 0)
1732 try cs.writeAll("]"); 1741 try cs.writeAll("]");
1733 1742
1734 var positional: ?Param(Id) = null; 1743 var has_positionals: bool = false;
1735 for (params) |param| { 1744 for (params) |param| {
1736 if (param.takes_value == .none and param.names.short != null) 1745 if (param.takes_value == .none and param.names.short != null)
1737 continue; 1746 continue;
@@ -1743,7 +1752,7 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi
1743 @ptrCast([*]const u8, s)[0..1] 1752 @ptrCast([*]const u8, s)[0..1]
1744 else 1753 else
1745 param.names.long orelse { 1754 param.names.long orelse {
1746 positional = param; 1755 has_positionals = true;
1747 continue; 1756 continue;
1748 }; 1757 };
1749 1758
@@ -1764,14 +1773,20 @@ pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !voi
1764 try cs.writeByte(']'); 1773 try cs.writeByte(']');
1765 } 1774 }
1766 1775
1767 if (positional) |p| { 1776 if (!has_positionals)
1777 return;
1778
1779 for (params) |param| {
1780 if (param.names.short != null or param.names.long != null)
1781 continue;
1782
1768 if (cos.bytes_written != 0) 1783 if (cos.bytes_written != 0)
1769 try cs.writeAll(" "); 1784 try cs.writeAll(" ");
1770 1785
1771 try cs.writeAll("<"); 1786 try cs.writeAll("<");
1772 try cs.writeAll(p.id.value()); 1787 try cs.writeAll(param.id.value());
1773 try cs.writeAll(">"); 1788 try cs.writeAll(">");
1774 if (p.takes_value == .many) 1789 if (param.takes_value == .many)
1775 try cs.writeAll("..."); 1790 try cs.writeAll("...");
1776 } 1791 }
1777} 1792}
@@ -1829,4 +1844,16 @@ test "usage" {
1829 \\ 1844 \\
1830 ), 1845 ),
1831 ); 1846 );
1847 try testUsage("<number> <file> <file>", &comptime parseParamsComptime(
1848 \\<number>
1849 \\<file>
1850 \\<file>
1851 \\
1852 ));
1853 try testUsage("<number> <outfile> <infile>...", &comptime parseParamsComptime(
1854 \\<number>
1855 \\<outfile>
1856 \\<infile>...
1857 \\
1858 ));
1832} 1859}