From 1d3d273524e3c180f015ff1e93c83075e4634e2c Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 25 Jan 2026 09:27:04 +1100 Subject: fix: use new `std.process.Init` args for Zig `0.16.0-dev.2261+d6b3dd25a` (#175) --- README.md | 90 +++++++++++++++++---------------------------------------------- 1 file changed, 24 insertions(+), 66 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index bbf1ea0..00ebe81 100644 --- a/README.md +++ b/README.md @@ -54,14 +54,7 @@ incomplete. The simplest way to use this library is to just call the `clap.parse` function. ```zig -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - +pub fn main(init: std.process.Init) !void { // First we specify what parameters our program can take. // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. const params = comptime clap.parseParamsComptime( @@ -76,12 +69,12 @@ pub fn main() !void { // This is optional. You can also pass `.{}` to `clap.parse` if you don't // care about the extra information `Diagnostics` provides. var diag = clap.Diagnostic{}; - var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ + var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ .diagnostic = &diag, - .allocator = gpa, + .allocator = init.gpa, }) catch |err| { // Report useful error and exit. - try diag.reportToFile(io, .stderr(), err); + try diag.reportToFile(init.io, .stderr(), err); return err; }; defer res.deinit(); @@ -112,14 +105,7 @@ contains a parser that returns `usize`. You can pass in something other than `cl if you want some other mapping. ```zig -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - +pub fn main(init: std.process.Init) !void { // First we specify what parameters our program can take. // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. const params = comptime clap.parseParamsComptime( @@ -142,15 +128,15 @@ pub fn main() !void { }; var diag = clap.Diagnostic{}; - var res = clap.parse(clap.Help, ¶ms, parsers, .{ + var res = clap.parse(clap.Help, ¶ms, parsers, init.minimal.args, .{ .diagnostic = &diag, - .allocator = gpa, + .allocator = init.gpa, // The assignment separator can be configured. `--number=1` and `--number:1` is now // allowed. .assignment_separators = "=:", }) catch |err| { // Report useful error and exit. - try diag.reportToFile(io, .stderr(), err); + try diag.reportToFile(init.io, .stderr(), err); return err; }; defer res.deinit(); @@ -198,15 +184,8 @@ const main_params = clap.parseParamsComptime( // get the return type of `clap.parse` and `clap.parseEx`. const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - - var iter = try std.process.ArgIterator.initWithAllocator(gpa); +pub fn main(init: std.process.Init) !void { + var iter = try init.minimal.args.iterateAllocator(init.gpa); defer iter.deinit(); _ = iter.next(); @@ -214,7 +193,7 @@ pub fn main() !void { var diag = clap.Diagnostic{}; var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ .diagnostic = &diag, - .allocator = gpa, + .allocator = init.gpa, // Terminate the parsing of arguments after parsing the first positional (0 is passed // here because parsed positionals are, like slices and arrays, indexed starting at 0). @@ -223,7 +202,7 @@ pub fn main() !void { // not fully consumed. It can then be reused to parse the arguments for subcommands. .terminating_positional = 0, }) catch |err| { - try diag.reportToFile(io, .stderr(), err); + try diag.reportToFile(init.io, .stderr(), err); return err; }; defer res.deinit(); @@ -234,11 +213,11 @@ pub fn main() !void { const command = res.positionals[0] orelse return error.MissingCommand; switch (command) { .help => std.debug.print("--help\n", .{}), - .math => try mathMain(io, gpa, &iter, res), + .math => try mathMain(init.io, init.gpa, &iter, res), } } -fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { +fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator, main_args: MainArgs) !void { // The parent arguments are not used here, but there are cases where it might be useful, so // this example shows how to pass the arguments around. _ = main_args; @@ -284,14 +263,7 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars `args.Iterator` to provide it with arguments lazily. ```zig -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - +pub fn main(init: std.process.Init) !void { // First we specify what parameters our program can take. const params = [_]clap.Param(u8){ .{ @@ -306,7 +278,7 @@ pub fn main() !void { .{ .id = 'f', .takes_value = .one }, }; - var iter = try std.process.ArgIterator.initWithAllocator(gpa); + var iter = try init.minimal.args.iterateAllocator(init.gpa); defer iter.deinit(); // Skip exe argument. @@ -316,7 +288,7 @@ pub fn main() !void { // This is optional. You can also leave the `diagnostic` field unset if you // don't care about the extra information `Diagnostic` provides. var diag = clap.Diagnostic{}; - var parser = clap.streaming.Clap(u8, std.process.ArgIterator){ + var parser = clap.streaming.Clap(u8, std.process.Args.Iterator){ .params = ¶ms, .iter = &iter, .diagnostic = &diag, @@ -325,7 +297,7 @@ pub fn main() !void { // Because we use a streaming parser, we have to consume each argument parsed individually. while (parser.next() catch |err| { // Report useful error and exit. - try diag.reportToFile(io, .stderr(), err); + try diag.reportToFile(init.io, .stderr(), err); return err; }) |arg| { // arg.param will point to the parameter which matched the argument. @@ -363,21 +335,14 @@ runtime. is passed to `help` to control how the help message is printed. ```zig -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - +pub fn main(init: std.process.Init) !void { const params = comptime clap.parseParamsComptime( \\-h, --help Display this help and exit. \\-v, --version Output version information and exit. \\ ); - var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); + var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa }); defer res.deinit(); // `clap.help` is a function that can print a simple help message. It can print any `Param` @@ -385,7 +350,7 @@ pub fn main() !void { // The last argument contains options as to how `help` should print those parameters. Using // `.{}` means the default options. if (res.args.help != 0) - return clap.helpToFile(io, .stderr(), clap.Help, ¶ms, .{}); + return clap.helpToFile(init.io, .stderr(), clap.Help, ¶ms, .{}); } const clap = @import("clap"); @@ -407,14 +372,7 @@ $ zig-out/bin/help --help `value` method so it can provide that in the output. ```zig -pub fn main() !void { - var gpa_state = std.heap.DebugAllocator(.{}){}; - const gpa = gpa_state.allocator(); - defer _ = gpa_state.deinit(); - - var threaded: std.Io.Threaded = .init_single_threaded; - const io: std.Io = threaded.io(); - +pub fn main(init: std.process.Init) !void { const params = comptime clap.parseParamsComptime( \\-h, --help Display this help and exit. \\-v, --version Output version information and exit. @@ -422,13 +380,13 @@ pub fn main() !void { \\ ); - var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); + var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, init.minimal.args, .{ .allocator = init.gpa }); defer res.deinit(); // `clap.usageToFile` is a function that can print a simple usage string. It can print any // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). if (res.args.help != 0) - return clap.usageToFile(io, .stdout(), clap.Help, ¶ms); + return clap.usageToFile(init.io, .stdout(), clap.Help, ¶ms); } const clap = @import("clap"); -- cgit v1.2.3