Skip to content

Commit 3b73a8e

Browse files
committed
chore: Update examples in README to use new std.Io
1 parent a202f72 commit 3b73a8e

7 files changed

Lines changed: 88 additions & 65 deletions

File tree

README.md

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ The simplest way to use this library is to just call the `clap.parse` function.
5555

5656
```zig
5757
pub 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, &params, 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
111115
pub 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, &params, 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
275287
pub 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
349366
pub 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, &params, clap.parsers.default, .{
360-
.allocator = gpa.allocator(),
361-
});
380+
var res = try clap.parse(clap.Help, &params, 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, &params, .{});
388+
return clap.helpToFile(io, .stderr(), clap.Help, &params, .{});
370389
}
371390
372391
const clap = @import("clap");
@@ -389,8 +408,12 @@ $ zig-out/bin/help --help
389408

390409
```zig
391410
pub 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, &params, clap.parsers.default, .{
403-
.allocator = gpa.allocator(),
404-
});
425+
var res = try clap.parse(clap.Help, &params, 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, &params);
431+
return clap.usageToFile(io, .stdout(), clap.Help, &params);
411432
}
412433
413434
const clap = @import("clap");

example/help.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
pub fn main() !void {
2-
var gpa = std.heap.DebugAllocator(.{}){};
3-
defer _ = gpa.deinit();
2+
var gpa_state = std.heap.DebugAllocator(.{}){};
3+
const gpa = gpa_state.allocator();
4+
defer _ = gpa_state.deinit();
5+
6+
var threaded: std.Io.Threaded = .init_single_threaded;
7+
const io: std.Io = threaded.io();
48

59
const params = comptime clap.parseParamsComptime(
610
\\-h, --help Display this help and exit.
711
\\-v, --version Output version information and exit.
812
\\
913
);
1014

11-
var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{
12-
.allocator = gpa.allocator(),
13-
});
15+
var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
1416
defer res.deinit();
1517

16-
var threaded: std.Io.Threaded = .init_single_threaded;
17-
const io: std.Io = threaded.io();
1818
// `clap.help` is a function that can print a simple help message. It can print any `Param`
1919
// where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
2020
// The last argument contains options as to how `help` should print those parameters. Using

example/simple-ex.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
pub fn main() !void {
2-
var gpa = std.heap.DebugAllocator(.{}){};
3-
defer _ = gpa.deinit();
2+
var gpa_state = std.heap.DebugAllocator(.{}){};
3+
const gpa = gpa_state.allocator();
4+
defer _ = gpa_state.deinit();
5+
6+
var threaded: std.Io.Threaded = .init_single_threaded;
7+
const io: std.Io = threaded.io();
48

59
// First we specify what parameters our program can take.
610
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -23,9 +27,6 @@ pub fn main() !void {
2327
.ANSWER = clap.parsers.enumeration(YesNo),
2428
};
2529

26-
var threaded: std.Io.Threaded = .init_single_threaded;
27-
const io: std.Io = threaded.io();
28-
2930
var diag = clap.Diagnostic{};
3031
var res = clap.parse(clap.Help, &params, parsers, .{
3132
.diagnostic = &diag,

example/simple.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
pub fn main() !void {
2-
var gpa = std.heap.DebugAllocator(.{}){};
3-
defer _ = gpa.deinit();
2+
var gpa_state = std.heap.DebugAllocator(.{}){};
3+
const gpa = gpa_state.allocator();
4+
defer _ = gpa_state.deinit();
5+
6+
var threaded: std.Io.Threaded = .init_single_threaded;
7+
const io: std.Io = threaded.io();
48

59
// First we specify what parameters our program can take.
610
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -16,11 +20,9 @@ pub fn main() !void {
1620
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
1721
// care about the extra information `Diagnostics` provides.
1822
var diag = clap.Diagnostic{};
19-
var threaded: std.Io.Threaded = .init_single_threaded;
20-
const io: std.Io = threaded.io();
2123
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
2224
.diagnostic = &diag,
23-
.allocator = gpa.allocator(),
25+
.allocator = gpa,
2426
}) catch |err| {
2527
// Report useful error and exit.
2628
try diag.reportToFile(io, .stderr(), err);

example/streaming-clap.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
pub fn main() !void {
2-
const allocator = std.heap.page_allocator;
2+
var gpa_state = std.heap.DebugAllocator(.{}){};
3+
const gpa = gpa_state.allocator();
4+
defer _ = gpa_state.deinit();
5+
6+
var threaded: std.Io.Threaded = .init_single_threaded;
7+
const io: std.Io = threaded.io();
38

49
// First we specify what parameters our program can take.
510
const params = [_]clap.Param(u8){
@@ -15,7 +20,7 @@ pub fn main() !void {
1520
.{ .id = 'f', .takes_value = .one },
1621
};
1722

18-
var iter = try std.process.ArgIterator.initWithAllocator(allocator);
23+
var iter = try std.process.ArgIterator.initWithAllocator(gpa);
1924
defer iter.deinit();
2025

2126
// Skip exe argument.
@@ -31,9 +36,6 @@ pub fn main() !void {
3136
.diagnostic = &diag,
3237
};
3338

34-
var threaded: std.Io.Threaded = .init_single_threaded;
35-
const io: std.Io = threaded.io();
36-
3739
// Because we use a streaming parser, we have to consume each argument parsed individually.
3840
while (parser.next() catch |err| {
3941
// Report useful error and exit.

example/subcommands.zig

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub fn main() !void {
2424
const gpa = gpa_state.allocator();
2525
defer _ = gpa_state.deinit();
2626

27+
var threaded: std.Io.Threaded = .init_single_threaded;
28+
const io: std.Io = threaded.io();
29+
2730
var iter = try std.process.ArgIterator.initWithAllocator(gpa);
2831
defer iter.deinit();
2932

3033
_ = iter.next();
3134

32-
var threaded: std.Io.Threaded = .init_single_threaded;
33-
const io: std.Io = threaded.io();
34-
3535
var diag = clap.Diagnostic{};
3636
var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
3737
.diagnostic = &diag,
@@ -55,11 +55,11 @@ pub fn main() !void {
5555
const command = res.positionals[0] orelse return error.MissingCommand;
5656
switch (command) {
5757
.help => std.debug.print("--help\n", .{}),
58-
.math => try mathMain(gpa, &iter, res),
58+
.math => try mathMain(io, gpa, &iter, res),
5959
}
6060
}
6161

62-
fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
62+
fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
6363
// The parent arguments are not used here, but there are cases where it might be useful, so
6464
// this example shows how to pass the arguments around.
6565
_ = main_args;
@@ -76,8 +76,6 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
7676

7777
// Here we pass the partially parsed argument iterator.
7878
var diag = clap.Diagnostic{};
79-
var threaded: std.Io.Threaded = .init_single_threaded;
80-
const io: std.Io = threaded.io();
8179
var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{
8280
.diagnostic = &diag,
8381
.allocator = gpa,

example/usage.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
pub fn main() !void {
2-
var gpa = std.heap.DebugAllocator(.{}){};
3-
defer _ = gpa.deinit();
2+
var gpa_state = std.heap.DebugAllocator(.{}){};
3+
const gpa = gpa_state.allocator();
4+
defer _ = gpa_state.deinit();
5+
6+
var threaded: std.Io.Threaded = .init_single_threaded;
7+
const io: std.Io = threaded.io();
48

59
const params = comptime clap.parseParamsComptime(
610
\\-h, --help Display this help and exit.
@@ -9,14 +13,9 @@ pub fn main() !void {
913
\\
1014
);
1115

12-
var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{
13-
.allocator = gpa.allocator(),
14-
});
16+
var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
1517
defer res.deinit();
1618

17-
var threaded: std.Io.Threaded = .init_single_threaded;
18-
const io: std.Io = threaded.io();
19-
2019
// `clap.usageToFile` is a function that can print a simple usage string. It can print any
2120
// `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
2221
if (res.args.help != 0)

0 commit comments

Comments
 (0)