@@ -55,8 +55,12 @@ The simplest way to use this library is to just call the `clap.parse` function.
5555
5656``` zig
5757pub fn main() !void {
58- var gpa = std.heap.DebugAllocator(.{}){};
59- defer _ = gpa.deinit();
58+ var gpa_state = std.heap.DebugAllocator(.{}){};
59+ const gpa = gpa_state.allocator();
60+ defer _ = gpa_state.deinit();
61+
62+ var threaded: std.Io.Threaded = .init_single_threaded;
63+ const io: std.Io = threaded.io();
6064
6165 // First we specify what parameters our program can take.
6266 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -70,14 +74,14 @@ pub fn main() !void {
7074
7175 // Initialize our diagnostics, which can be used for reporting useful errors.
7276 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
73- // care about the extra information `Diagnostic ` provides.
77+ // care about the extra information `Diagnostics ` provides.
7478 var diag = clap.Diagnostic{};
7579 var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{
7680 .diagnostic = &diag,
77- .allocator = gpa.allocator() ,
81+ .allocator = gpa,
7882 }) catch |err| {
7983 // Report useful error and exit.
80- try diag.reportToFile(.stderr(), err);
84+ try diag.reportToFile(io, .stderr(), err);
8185 return err;
8286 };
8387 defer res.deinit();
@@ -109,8 +113,12 @@ if you want some other mapping.
109113
110114``` zig
111115pub fn main() !void {
112- var gpa = std.heap.DebugAllocator(.{}){};
113- defer _ = gpa.deinit();
116+ var gpa_state = std.heap.DebugAllocator(.{}){};
117+ const gpa = gpa_state.allocator();
118+ defer _ = gpa_state.deinit();
119+
120+ var threaded: std.Io.Threaded = .init_single_threaded;
121+ const io: std.Io = threaded.io();
114122
115123 // First we specify what parameters our program can take.
116124 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -136,12 +144,13 @@ pub fn main() !void {
136144 var diag = clap.Diagnostic{};
137145 var res = clap.parse(clap.Help, ¶ms, parsers, .{
138146 .diagnostic = &diag,
139- .allocator = gpa.allocator() ,
147+ .allocator = gpa,
140148 // The assignment separator can be configured. `--number=1` and `--number:1` is now
141149 // allowed.
142150 .assignment_separators = "=:",
143151 }) catch |err| {
144- try diag.reportToFile(.stderr(), err);
152+ // Report useful error and exit.
153+ try diag.reportToFile(io, .stderr(), err);
145154 return err;
146155 };
147156 defer res.deinit();
@@ -194,6 +203,9 @@ pub fn main() !void {
194203 const gpa = gpa_state.allocator();
195204 defer _ = gpa_state.deinit();
196205
206+ var threaded: std.Io.Threaded = .init_single_threaded;
207+ const io: std.Io = threaded.io();
208+
197209 var iter = try std.process.ArgIterator.initWithAllocator(gpa);
198210 defer iter.deinit();
199211
@@ -211,7 +223,7 @@ pub fn main() !void {
211223 // not fully consumed. It can then be reused to parse the arguments for subcommands.
212224 .terminating_positional = 0,
213225 }) catch |err| {
214- try diag.reportToFile(.stderr(), err);
226+ try diag.reportToFile(io, .stderr(), err);
215227 return err;
216228 };
217229 defer res.deinit();
@@ -222,11 +234,11 @@ pub fn main() !void {
222234 const command = res.positionals[0] orelse return error.MissingCommand;
223235 switch (command) {
224236 .help => std.debug.print("--help\n", .{}),
225- .math => try mathMain(gpa, &iter, res),
237+ .math => try mathMain(io, gpa, &iter, res),
226238 }
227239}
228240
229- fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
241+ fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
230242 // The parent arguments are not used here, but there are cases where it might be useful, so
231243 // this example shows how to pass the arguments around.
232244 _ = main_args;
@@ -247,7 +259,7 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
247259 .diagnostic = &diag,
248260 .allocator = gpa,
249261 }) catch |err| {
250- try diag.reportToFile(.stderr(), err);
262+ try diag.reportToFile(io, .stderr(), err);
251263 return err; // propagate error
252264 };
253265 defer res.deinit();
@@ -273,7 +285,12 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars
273285
274286``` zig
275287pub fn main() !void {
276- const allocator = std.heap.page_allocator;
288+ var gpa_state = std.heap.DebugAllocator(.{}){};
289+ const gpa = gpa_state.allocator();
290+ defer _ = gpa_state.deinit();
291+
292+ var threaded: std.Io.Threaded = .init_single_threaded;
293+ const io: std.Io = threaded.io();
277294
278295 // First we specify what parameters our program can take.
279296 const params = [_]clap.Param(u8){
@@ -289,7 +306,7 @@ pub fn main() !void {
289306 .{ .id = 'f', .takes_value = .one },
290307 };
291308
292- var iter = try std.process.ArgIterator.initWithAllocator(allocator );
309+ var iter = try std.process.ArgIterator.initWithAllocator(gpa );
293310 defer iter.deinit();
294311
295312 // Skip exe argument.
@@ -308,7 +325,7 @@ pub fn main() !void {
308325 // Because we use a streaming parser, we have to consume each argument parsed individually.
309326 while (parser.next() catch |err| {
310327 // Report useful error and exit.
311- try diag.reportToFile(.stderr(), err);
328+ try diag.reportToFile(io, .stderr(), err);
312329 return err;
313330 }) |arg| {
314331 // arg.param will point to the parameter which matched the argument.
@@ -347,26 +364,28 @@ is passed to `help` to control how the help message is printed.
347364
348365``` zig
349366pub fn main() !void {
350- var gpa = std.heap.DebugAllocator(.{}){};
351- defer _ = gpa.deinit();
367+ var gpa_state = std.heap.DebugAllocator(.{}){};
368+ const gpa = gpa_state.allocator();
369+ defer _ = gpa_state.deinit();
370+
371+ var threaded: std.Io.Threaded = .init_single_threaded;
372+ const io: std.Io = threaded.io();
352373
353374 const params = comptime clap.parseParamsComptime(
354375 \\-h, --help Display this help and exit.
355376 \\-v, --version Output version information and exit.
356377 \\
357378 );
358379
359- var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{
360- .allocator = gpa.allocator(),
361- });
380+ var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa });
362381 defer res.deinit();
363382
364383 // `clap.help` is a function that can print a simple help message. It can print any `Param`
365384 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
366385 // The last argument contains options as to how `help` should print those parameters. Using
367386 // `.{}` means the default options.
368387 if (res.args.help != 0)
369- return clap.helpToFile(.stderr(), clap.Help, ¶ms, .{});
388+ return clap.helpToFile(io, .stderr(), clap.Help, ¶ms, .{});
370389}
371390
372391const clap = @import("clap");
@@ -389,8 +408,12 @@ $ zig-out/bin/help --help
389408
390409``` zig
391410pub fn main() !void {
392- var gpa = std.heap.DebugAllocator(.{}){};
393- defer _ = gpa.deinit();
411+ var gpa_state = std.heap.DebugAllocator(.{}){};
412+ const gpa = gpa_state.allocator();
413+ defer _ = gpa_state.deinit();
414+
415+ var threaded: std.Io.Threaded = .init_single_threaded;
416+ const io: std.Io = threaded.io();
394417
395418 const params = comptime clap.parseParamsComptime(
396419 \\-h, --help Display this help and exit.
@@ -399,15 +422,13 @@ pub fn main() !void {
399422 \\
400423 );
401424
402- var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{
403- .allocator = gpa.allocator(),
404- });
425+ var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa });
405426 defer res.deinit();
406427
407428 // `clap.usageToFile` is a function that can print a simple usage string. It can print any
408429 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
409430 if (res.args.help != 0)
410- return clap.usageToFile(.stdout(), clap.Help, ¶ms);
431+ return clap.usageToFile(io, .stdout(), clap.Help, ¶ms);
411432}
412433
413434const clap = @import("clap");
0 commit comments