diff options
| author | 2026-01-02 20:59:20 +0900 | |
|---|---|---|
| committer | 2026-01-02 20:59:20 +0900 | |
| commit | 3c35ff18bb32fec2f53e12e6bdbb00422d6c33fe (patch) | |
| tree | 71b3c0023650979eceee27bc37f507d2e73a7e17 | |
| parent | hotfix: replace deprecated std.fs.File with std.Io.File for Zig v0.16 (diff) | |
| download | zig-clap-3c35ff18bb32fec2f53e12e6bdbb00422d6c33fe.tar.gz zig-clap-3c35ff18bb32fec2f53e12e6bdbb00422d6c33fe.tar.xz zig-clap-3c35ff18bb32fec2f53e12e6bdbb00422d6c33fe.zip | |
chore: update examples to use std.Io for Zig v0.16 compatibility
Signed-off-by: Takumi Katase <takumi.katase@devoc.ninja>
| -rw-r--r-- | example/help.zig | 4 | ||||
| -rw-r--r-- | example/simple-ex.zig | 5 | ||||
| -rw-r--r-- | example/simple.zig | 4 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 5 | ||||
| -rw-r--r-- | example/subcommands.zig | 9 | ||||
| -rw-r--r-- | example/usage.zig | 5 |
6 files changed, 25 insertions, 7 deletions
diff --git a/example/help.zig b/example/help.zig index d8e8f12..a4379de 100644 --- a/example/help.zig +++ b/example/help.zig | |||
| @@ -13,12 +13,14 @@ pub fn main() !void { | |||
| 13 | }); | 13 | }); |
| 14 | defer res.deinit(); | 14 | defer res.deinit(); |
| 15 | 15 | ||
| 16 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 17 | const io: std.Io = threaded.io(); | ||
| 16 | // `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` |
| 17 | // 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). |
| 18 | // 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 |
| 19 | // `.{}` means the default options. | 21 | // `.{}` means the default options. |
| 20 | if (res.args.help != 0) | 22 | if (res.args.help != 0) |
| 21 | return clap.helpToFile(.stderr(), clap.Help, ¶ms, .{}); | 23 | return clap.helpToFile(io, .stderr(), clap.Help, ¶ms, .{}); |
| 22 | } | 24 | } |
| 23 | 25 | ||
| 24 | const clap = @import("clap"); | 26 | const clap = @import("clap"); |
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index 912c75b..7ab8d1e 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.zig | |||
| @@ -23,6 +23,9 @@ pub fn main() !void { | |||
| 23 | .ANSWER = clap.parsers.enumeration(YesNo), | 23 | .ANSWER = clap.parsers.enumeration(YesNo), |
| 24 | }; | 24 | }; |
| 25 | 25 | ||
| 26 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 27 | const io: std.Io = threaded.io(); | ||
| 28 | |||
| 26 | var diag = clap.Diagnostic{}; | 29 | var diag = clap.Diagnostic{}; |
| 27 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ | 30 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 28 | .diagnostic = &diag, | 31 | .diagnostic = &diag, |
| @@ -32,7 +35,7 @@ pub fn main() !void { | |||
| 32 | .assignment_separators = "=:", | 35 | .assignment_separators = "=:", |
| 33 | }) catch |err| { | 36 | }) catch |err| { |
| 34 | // Report useful error and exit. | 37 | // Report useful error and exit. |
| 35 | try diag.reportToFile(.stderr(), err); | 38 | try diag.reportToFile(io, .stderr(), err); |
| 36 | return err; | 39 | return err; |
| 37 | }; | 40 | }; |
| 38 | defer res.deinit(); | 41 | defer res.deinit(); |
diff --git a/example/simple.zig b/example/simple.zig index abcf448..67313aa 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -16,12 +16,14 @@ pub fn main() !void { | |||
| 16 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 16 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 17 | // care about the extra information `Diagnostics` provides. | 17 | // care about the extra information `Diagnostics` provides. |
| 18 | var diag = clap.Diagnostic{}; | 18 | var diag = clap.Diagnostic{}; |
| 19 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 20 | const io: std.Io = threaded.io(); | ||
| 19 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 21 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 20 | .diagnostic = &diag, | 22 | .diagnostic = &diag, |
| 21 | .allocator = gpa.allocator(), | 23 | .allocator = gpa.allocator(), |
| 22 | }) catch |err| { | 24 | }) catch |err| { |
| 23 | // Report useful error and exit. | 25 | // Report useful error and exit. |
| 24 | try diag.reportToFile(.stderr(), err); | 26 | try diag.reportToFile(io, .stderr(), err); |
| 25 | return err; | 27 | return err; |
| 26 | }; | 28 | }; |
| 27 | defer res.deinit(); | 29 | defer res.deinit(); |
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 4cd6962..c58cd24 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -31,10 +31,13 @@ pub fn main() !void { | |||
| 31 | .diagnostic = &diag, | 31 | .diagnostic = &diag, |
| 32 | }; | 32 | }; |
| 33 | 33 | ||
| 34 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 35 | const io: std.Io = threaded.io(); | ||
| 36 | |||
| 34 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 37 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 35 | while (parser.next() catch |err| { | 38 | while (parser.next() catch |err| { |
| 36 | // Report useful error and exit. | 39 | // Report useful error and exit. |
| 37 | try diag.reportToFile(.stderr(), err); | 40 | try diag.reportToFile(io, .stderr(), err); |
| 38 | return err; | 41 | return err; |
| 39 | }) |arg| { | 42 | }) |arg| { |
| 40 | // arg.param will point to the parameter which matched the argument. | 43 | // arg.param will point to the parameter which matched the argument. |
diff --git a/example/subcommands.zig b/example/subcommands.zig index 5c3e7c7..6eb919b 100644 --- a/example/subcommands.zig +++ b/example/subcommands.zig | |||
| @@ -29,6 +29,9 @@ pub fn main() !void { | |||
| 29 | 29 | ||
| 30 | _ = iter.next(); | 30 | _ = iter.next(); |
| 31 | 31 | ||
| 32 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 33 | const io: std.Io = threaded.io(); | ||
| 34 | |||
| 32 | var diag = clap.Diagnostic{}; | 35 | var diag = clap.Diagnostic{}; |
| 33 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ | 36 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ |
| 34 | .diagnostic = &diag, | 37 | .diagnostic = &diag, |
| @@ -41,7 +44,7 @@ pub fn main() !void { | |||
| 41 | // not fully consumed. It can then be reused to parse the arguments for subcommands. | 44 | // not fully consumed. It can then be reused to parse the arguments for subcommands. |
| 42 | .terminating_positional = 0, | 45 | .terminating_positional = 0, |
| 43 | }) catch |err| { | 46 | }) catch |err| { |
| 44 | try diag.reportToFile(.stderr(), err); | 47 | try diag.reportToFile(io, .stderr(), err); |
| 45 | return err; | 48 | return err; |
| 46 | }; | 49 | }; |
| 47 | defer res.deinit(); | 50 | defer res.deinit(); |
| @@ -73,11 +76,13 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M | |||
| 73 | 76 | ||
| 74 | // Here we pass the partially parsed argument iterator. | 77 | // Here we pass the partially parsed argument iterator. |
| 75 | 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(); | ||
| 76 | var res = clap.parseEx(clap.Help, ¶ms, clap.parsers.default, iter, .{ | 81 | var res = clap.parseEx(clap.Help, ¶ms, clap.parsers.default, iter, .{ |
| 77 | .diagnostic = &diag, | 82 | .diagnostic = &diag, |
| 78 | .allocator = gpa, | 83 | .allocator = gpa, |
| 79 | }) catch |err| { | 84 | }) catch |err| { |
| 80 | try diag.reportToFile(.stderr(), err); | 85 | try diag.reportToFile(io, .stderr(), err); |
| 81 | return err; // propagate error | 86 | return err; // propagate error |
| 82 | }; | 87 | }; |
| 83 | defer res.deinit(); | 88 | defer res.deinit(); |
diff --git a/example/usage.zig b/example/usage.zig index aab9cb6..c1440b9 100644 --- a/example/usage.zig +++ b/example/usage.zig | |||
| @@ -14,10 +14,13 @@ pub fn main() !void { | |||
| 14 | }); | 14 | }); |
| 15 | defer res.deinit(); | 15 | defer res.deinit(); |
| 16 | 16 | ||
| 17 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 18 | const io: std.Io = threaded.io(); | ||
| 19 | |||
| 17 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any | 20 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any |
| 18 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). | 21 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). |
| 19 | if (res.args.help != 0) | 22 | if (res.args.help != 0) |
| 20 | return clap.usageToFile(.stdout(), clap.Help, ¶ms); | 23 | return clap.usageToFile(io, .stdout(), clap.Help, ¶ms); |
| 21 | } | 24 | } |
| 22 | 25 | ||
| 23 | const clap = @import("clap"); | 26 | const clap = @import("clap"); |