From 0356f53d5bc46011efc6b6ffbfe0b202466d7b4f Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Tue, 22 Jul 2025 10:54:19 +0200 Subject: feat: Add `clap.usageToFile` The code for printing usage became quite verbose after writegate. Writing usage to a file like stdout and stderr is very common, so this wrapper provides a default, buffered way to report to a file. --- clap.zig | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'clap.zig') diff --git a/clap.zig b/clap.zig index 4a001ce..1e710ea 100644 --- a/clap.zig +++ b/clap.zig @@ -569,7 +569,7 @@ pub const Diagnostic = struct { var buf: [1024]u8 = undefined; var writer = file.writer(&buf); try diag.report(&writer.interface, err); - return writer.end(); + return writer.interface.flush(); } }; @@ -1377,7 +1377,7 @@ pub fn helpToFile( var buf: [1024]u8 = undefined; var writer = file.writer(&buf); try help(&writer.interface, Id, params, opt); - return writer.end(); + return writer.interface.flush(); } /// Print a slice of `Param` formatted as a help string to `writer`. This function expects @@ -2027,6 +2027,14 @@ test "clap.help" { ); } +/// Wrapper around `usage`, which writes to a file in a buffered manner +pub fn usageToFile(file: std.fs.File, comptime Id: type, params: []const Param(Id)) !void { + var buf: [1024]u8 = undefined; + var writer = file.writer(&buf); + try usage(&writer.interface, Id, params); + return writer.interface.flush(); +} + /// Will print a usage message in the following format: /// [-abc] [--longa] [-d ] [--longb ] /// -- cgit v1.2.3