summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--clap.zig12
-rw-r--r--example/usage.zig13
3 files changed, 18 insertions, 19 deletions
diff --git a/README.md b/README.md
index 6a1435f..b9fbda7 100644
--- a/README.md
+++ b/README.md
@@ -404,14 +404,10 @@ pub fn main() !void {
404 }); 404 });
405 defer res.deinit(); 405 defer res.deinit();
406 406
407 // `clap.usage` is a function that can print a simple help message. It can print any `Param` 407 // `clap.usageToFile` is a function that can print a simple usage string. It can print any
408 // where `Id` has a `value` method (`Param(Help)` is one such parameter). 408 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
409 if (res.args.help != 0) { 409 if (res.args.help != 0)
410 var buf: [1024]u8 = undefined; 410 return clap.usage(.stdout(), clap.Help, &params);
411 var stderr = std.fs.File.stderr().writer(&buf);
412 try clap.usage(&stderr.interface, clap.Help, &params);
413 return stderr.interface.flush();
414 }
415} 411}
416 412
417const clap = @import("clap"); 413const clap = @import("clap");
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 {
569 var buf: [1024]u8 = undefined; 569 var buf: [1024]u8 = undefined;
570 var writer = file.writer(&buf); 570 var writer = file.writer(&buf);
571 try diag.report(&writer.interface, err); 571 try diag.report(&writer.interface, err);
572 return writer.end(); 572 return writer.interface.flush();
573 } 573 }
574}; 574};
575 575
@@ -1377,7 +1377,7 @@ pub fn helpToFile(
1377 var buf: [1024]u8 = undefined; 1377 var buf: [1024]u8 = undefined;
1378 var writer = file.writer(&buf); 1378 var writer = file.writer(&buf);
1379 try help(&writer.interface, Id, params, opt); 1379 try help(&writer.interface, Id, params, opt);
1380 return writer.end(); 1380 return writer.interface.flush();
1381} 1381}
1382 1382
1383/// Print a slice of `Param` formatted as a help string to `writer`. This function expects 1383/// Print a slice of `Param` formatted as a help string to `writer`. This function expects
@@ -2027,6 +2027,14 @@ test "clap.help" {
2027 ); 2027 );
2028} 2028}
2029 2029
2030/// Wrapper around `usage`, which writes to a file in a buffered manner
2031pub fn usageToFile(file: std.fs.File, comptime Id: type, params: []const Param(Id)) !void {
2032 var buf: [1024]u8 = undefined;
2033 var writer = file.writer(&buf);
2034 try usage(&writer.interface, Id, params);
2035 return writer.interface.flush();
2036}
2037
2030/// Will print a usage message in the following format: 2038/// Will print a usage message in the following format:
2031/// [-abc] [--longa] [-d <T>] [--longb <T>] <T> 2039/// [-abc] [--longa] [-d <T>] [--longb <T>] <T>
2032/// 2040///
diff --git a/example/usage.zig b/example/usage.zig
index 8bd25b7..b7308f5 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -14,15 +14,10 @@ pub fn main() !void {
14 }); 14 });
15 defer res.deinit(); 15 defer res.deinit();
16 16
17 // `clap.usage` is a function that can print a simple help message. It can print any `Param` 17 // `clap.usageToFile` is a function that can print a simple usage string. It can print any
18 // where `Id` has a `value` method (`Param(Help)` is one such parameter). 18 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
19 if (res.args.help != 0) { 19 if (res.args.help != 0)
20 var buf: [1024]u8 = undefined; 20 return clap.usageToFile(.stdout(), clap.Help, &params);
21 var stderr = std.fs.File.stderr().writer(&buf);
22 clap.usage(&stderr.interface, clap.Help, &params) catch {};
23 try stderr.interface.flush();
24 return;
25 }
26} 21}
27 22
28const clap = @import("clap"); 23const clap = @import("clap");