summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2026-01-07 21:01:09 +0100
committerGravatar Jimmi Holst Christensen2026-01-07 21:01:09 +0100
commit3b73a8edaff14039d917b1958715d1d54659851b (patch)
treea4c378b8678ba76685cfd4af3be2904b62104ab1 /example
parentchore: update minimum_zig_version (diff)
downloadzig-clap-3b73a8edaff14039d917b1958715d1d54659851b.tar.gz
zig-clap-3b73a8edaff14039d917b1958715d1d54659851b.tar.xz
zig-clap-3b73a8edaff14039d917b1958715d1d54659851b.zip
chore: Update examples in README to use new std.Io
Diffstat (limited to 'example')
-rw-r--r--example/help.zig14
-rw-r--r--example/simple-ex.zig11
-rw-r--r--example/simple.zig12
-rw-r--r--example/streaming-clap.zig12
-rw-r--r--example/subcommands.zig12
-rw-r--r--example/usage.zig15
6 files changed, 39 insertions, 37 deletions
diff --git a/example/help.zig b/example/help.zig
index a4379de..b07bc52 100644
--- a/example/help.zig
+++ b/example/help.zig
@@ -1,6 +1,10 @@
1pub fn main() !void { 1pub fn main() !void {
2 var gpa = std.heap.DebugAllocator(.{}){}; 2 var gpa_state = std.heap.DebugAllocator(.{}){};
3 defer _ = gpa.deinit(); 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();
4 8
5 const params = comptime clap.parseParamsComptime( 9 const params = comptime clap.parseParamsComptime(
6 \\-h, --help Display this help and exit. 10 \\-h, --help Display this help and exit.
@@ -8,13 +12,9 @@ pub fn main() !void {
8 \\ 12 \\
9 ); 13 );
10 14
11 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ 15 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
12 .allocator = gpa.allocator(),
13 });
14 defer res.deinit(); 16 defer res.deinit();
15 17
16 var threaded: std.Io.Threaded = .init_single_threaded;
17 const io: std.Io = threaded.io();
18 // `clap.help` is a function that can print a simple help message. It can print any `Param` 18 // `clap.help` is a function that can print a simple help message. It can print any `Param`
19 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter). 19 // where `Id` has a `description` and `value` method (`Param(Help)` is one such parameter).
20 // The last argument contains options as to how `help` should print those parameters. Using 20 // The last argument contains options as to how `help` should print those parameters. Using
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index 7ab8d1e..f6a1c7f 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -1,6 +1,10 @@
1pub fn main() !void { 1pub fn main() !void {
2 var gpa = std.heap.DebugAllocator(.{}){}; 2 var gpa_state = std.heap.DebugAllocator(.{}){};
3 defer _ = gpa.deinit(); 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();
4 8
5 // First we specify what parameters our program can take. 9 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. 10 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -23,9 +27,6 @@ pub fn main() !void {
23 .ANSWER = clap.parsers.enumeration(YesNo), 27 .ANSWER = clap.parsers.enumeration(YesNo),
24 }; 28 };
25 29
26 var threaded: std.Io.Threaded = .init_single_threaded;
27 const io: std.Io = threaded.io();
28
29 var diag = clap.Diagnostic{}; 30 var diag = clap.Diagnostic{};
30 var res = clap.parse(clap.Help, &params, parsers, .{ 31 var res = clap.parse(clap.Help, &params, parsers, .{
31 .diagnostic = &diag, 32 .diagnostic = &diag,
diff --git a/example/simple.zig b/example/simple.zig
index 67313aa..a7772c6 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -1,6 +1,10 @@
1pub fn main() !void { 1pub fn main() !void {
2 var gpa = std.heap.DebugAllocator(.{}){}; 2 var gpa_state = std.heap.DebugAllocator(.{}){};
3 defer _ = gpa.deinit(); 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();
4 8
5 // First we specify what parameters our program can take. 9 // First we specify what parameters our program can take.
6 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. 10 // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
@@ -16,11 +20,9 @@ pub fn main() !void {
16 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 20 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
17 // care about the extra information `Diagnostics` provides. 21 // care about the extra information `Diagnostics` provides.
18 var diag = clap.Diagnostic{}; 22 var diag = clap.Diagnostic{};
19 var threaded: std.Io.Threaded = .init_single_threaded;
20 const io: std.Io = threaded.io();
21 var res = clap.parse(clap.Help, &params, clap.parsers.default, .{ 23 var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
22 .diagnostic = &diag, 24 .diagnostic = &diag,
23 .allocator = gpa.allocator(), 25 .allocator = gpa,
24 }) catch |err| { 26 }) catch |err| {
25 // Report useful error and exit. 27 // Report useful error and exit.
26 try diag.reportToFile(io, .stderr(), err); 28 try diag.reportToFile(io, .stderr(), err);
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index c58cd24..9d0a3ca 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -1,5 +1,10 @@
1pub fn main() !void { 1pub 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();
3 8
4 // First we specify what parameters our program can take. 9 // First we specify what parameters our program can take.
5 const params = [_]clap.Param(u8){ 10 const params = [_]clap.Param(u8){
@@ -15,7 +20,7 @@ pub fn main() !void {
15 .{ .id = 'f', .takes_value = .one }, 20 .{ .id = 'f', .takes_value = .one },
16 }; 21 };
17 22
18 var iter = try std.process.ArgIterator.initWithAllocator(allocator); 23 var iter = try std.process.ArgIterator.initWithAllocator(gpa);
19 defer iter.deinit(); 24 defer iter.deinit();
20 25
21 // Skip exe argument. 26 // Skip exe argument.
@@ -31,9 +36,6 @@ pub fn main() !void {
31 .diagnostic = &diag, 36 .diagnostic = &diag,
32 }; 37 };
33 38
34 var threaded: std.Io.Threaded = .init_single_threaded;
35 const io: std.Io = threaded.io();
36
37 // Because we use a streaming parser, we have to consume each argument parsed individually. 39 // Because we use a streaming parser, we have to consume each argument parsed individually.
38 while (parser.next() catch |err| { 40 while (parser.next() catch |err| {
39 // Report useful error and exit. 41 // Report useful error and exit.
diff --git a/example/subcommands.zig b/example/subcommands.zig
index 6eb919b..bd8625b 100644
--- a/example/subcommands.zig
+++ b/example/subcommands.zig
@@ -24,14 +24,14 @@ pub fn main() !void {
24 const gpa = gpa_state.allocator(); 24 const gpa = gpa_state.allocator();
25 defer _ = gpa_state.deinit(); 25 defer _ = gpa_state.deinit();
26 26
27 var threaded: std.Io.Threaded = .init_single_threaded;
28 const io: std.Io = threaded.io();
29
27 var iter = try std.process.ArgIterator.initWithAllocator(gpa); 30 var iter = try std.process.ArgIterator.initWithAllocator(gpa);
28 defer iter.deinit(); 31 defer iter.deinit();
29 32
30 _ = iter.next(); 33 _ = iter.next();
31 34
32 var threaded: std.Io.Threaded = .init_single_threaded;
33 const io: std.Io = threaded.io();
34
35 var diag = clap.Diagnostic{}; 35 var diag = clap.Diagnostic{};
36 var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ 36 var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{
37 .diagnostic = &diag, 37 .diagnostic = &diag,
@@ -55,11 +55,11 @@ pub fn main() !void {
55 const command = res.positionals[0] orelse return error.MissingCommand; 55 const command = res.positionals[0] orelse return error.MissingCommand;
56 switch (command) { 56 switch (command) {
57 .help => std.debug.print("--help\n", .{}), 57 .help => std.debug.print("--help\n", .{}),
58 .math => try mathMain(gpa, &iter, res), 58 .math => try mathMain(io, gpa, &iter, res),
59 } 59 }
60} 60}
61 61
62fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { 62fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void {
63 // The parent arguments are not used here, but there are cases where it might be useful, so 63 // The parent arguments are not used here, but there are cases where it might be useful, so
64 // this example shows how to pass the arguments around. 64 // this example shows how to pass the arguments around.
65 _ = main_args; 65 _ = main_args;
@@ -76,8 +76,6 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M
76 76
77 // Here we pass the partially parsed argument iterator. 77 // Here we pass the partially parsed argument iterator.
78 var diag = clap.Diagnostic{}; 78 var diag = clap.Diagnostic{};
79 var threaded: std.Io.Threaded = .init_single_threaded;
80 const io: std.Io = threaded.io();
81 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{ 79 var res = clap.parseEx(clap.Help, &params, clap.parsers.default, iter, .{
82 .diagnostic = &diag, 80 .diagnostic = &diag,
83 .allocator = gpa, 81 .allocator = gpa,
diff --git a/example/usage.zig b/example/usage.zig
index c1440b9..2d51cc4 100644
--- a/example/usage.zig
+++ b/example/usage.zig
@@ -1,6 +1,10 @@
1pub fn main() !void { 1pub fn main() !void {
2 var gpa = std.heap.DebugAllocator(.{}){}; 2 var gpa_state = std.heap.DebugAllocator(.{}){};
3 defer _ = gpa.deinit(); 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();
4 8
5 const params = comptime clap.parseParamsComptime( 9 const params = comptime clap.parseParamsComptime(
6 \\-h, --help Display this help and exit. 10 \\-h, --help Display this help and exit.
@@ -9,14 +13,9 @@ pub fn main() !void {
9 \\ 13 \\
10 ); 14 );
11 15
12 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ 16 var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{ .allocator = gpa });
13 .allocator = gpa.allocator(),
14 });
15 defer res.deinit(); 17 defer res.deinit();
16 18
17 var threaded: std.Io.Threaded = .init_single_threaded;
18 const io: std.Io = threaded.io();
19
20 // `clap.usageToFile` is a function that can print a simple usage string. It can print any 19 // `clap.usageToFile` is a function that can print a simple usage string. It can print any
21 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). 20 // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter).
22 if (res.args.help != 0) 21 if (res.args.help != 0)