diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | clap.zig | 13 | ||||
| -rw-r--r-- | example/help.zig | 8 |
3 files changed, 17 insertions, 12 deletions
| @@ -365,12 +365,8 @@ pub fn main() !void { | |||
| 365 | // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). | 365 | // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). |
| 366 | // The last argument contains options as to how `help` should print those parameters. Using | 366 | // The last argument contains options as to how `help` should print those parameters. Using |
| 367 | // `.{}` means the default options. | 367 | // `.{}` means the default options. |
| 368 | if (res.args.help != 0) { | 368 | if (res.args.help != 0) |
| 369 | var buf: [1024]u8 = undefined; | 369 | return clap.helpToFile(.stderr(), clap.Help, ¶ms, .{}); |
| 370 | var stderr = std.fs.File.stderr().writer(&buf); | ||
| 371 | try clap.help(&stderr.interface, clap.Help, ¶ms, .{}); | ||
| 372 | return stderr.interface.flush(); | ||
| 373 | } | ||
| 374 | } | 370 | } |
| 375 | 371 | ||
| 376 | const clap = @import("clap"); | 372 | const clap = @import("clap"); |
| @@ -1367,6 +1367,19 @@ pub const HelpOptions = struct { | |||
| 1367 | spacing_between_parameters: usize = 1, | 1367 | spacing_between_parameters: usize = 1, |
| 1368 | }; | 1368 | }; |
| 1369 | 1369 | ||
| 1370 | /// Wrapper around `help`, which writes to a file in a buffered manner | ||
| 1371 | pub fn helpToFile( | ||
| 1372 | file: std.fs.File, | ||
| 1373 | comptime Id: type, | ||
| 1374 | params: []const Param(Id), | ||
| 1375 | opt: HelpOptions, | ||
| 1376 | ) !void { | ||
| 1377 | var buf: [1024]u8 = undefined; | ||
| 1378 | var writer = file.writer(&buf); | ||
| 1379 | try help(&writer.interface, Id, params, opt); | ||
| 1380 | return writer.end(); | ||
| 1381 | } | ||
| 1382 | |||
| 1370 | /// 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 |
| 1371 | /// `Id` to have the methods `description` and `value` which are used by `help` to describe | 1384 | /// `Id` to have the methods `description` and `value` which are used by `help` to describe |
| 1372 | /// each parameter. Using `Help` as `Id` is good choice. | 1385 | /// each parameter. Using `Help` as `Id` is good choice. |
diff --git a/example/help.zig b/example/help.zig index 676a56a..5c88d83 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -17,12 +17,8 @@ pub fn main() !void { | |||
| 17 | // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). | 17 | // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). |
| 18 | // The last argument contains options as to how `help` should print those parameters. Using | 18 | // The last argument contains options as to how `help` should print those parameters. Using |
| 19 | // `.{}` means the default options. | 19 | // `.{}` means the default options. |
| 20 | if (res.args.help != 0) { | 20 | if (res.args.help != 0) |
| 21 | var buf: [1024]u8 = undefined; | 21 | return clap.helpToFile(.stderr(), clap.Help, ¶ms, .{}); |
| 22 | var stderr = std.fs.File.stderr().writer(&buf); | ||
| 23 | try clap.help(&stderr.interface, clap.Help, ¶ms, .{}); | ||
| 24 | return stderr.interface.flush(); | ||
| 25 | } | ||
| 26 | } | 22 | } |
| 27 | 23 | ||
| 28 | const clap = @import("clap"); | 24 | const clap = @import("clap"); |