diff options
| author | 2026-01-07 21:01:09 +0100 | |
|---|---|---|
| committer | 2026-01-07 21:01:09 +0100 | |
| commit | 3b73a8edaff14039d917b1958715d1d54659851b (patch) | |
| tree | a4c378b8678ba76685cfd4af3be2904b62104ab1 | |
| parent | chore: update minimum_zig_version (diff) | |
| download | zig-clap-3b73a8edaff14039d917b1958715d1d54659851b.tar.gz zig-clap-3b73a8edaff14039d917b1958715d1d54659851b.tar.xz zig-clap-3b73a8edaff14039d917b1958715d1d54659851b.zip | |
chore: Update examples in README to use new std.Io
| -rw-r--r-- | README.md | 77 | ||||
| -rw-r--r-- | example/help.zig | 14 | ||||
| -rw-r--r-- | example/simple-ex.zig | 11 | ||||
| -rw-r--r-- | example/simple.zig | 12 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 12 | ||||
| -rw-r--r-- | example/subcommands.zig | 12 | ||||
| -rw-r--r-- | example/usage.zig | 15 |
7 files changed, 88 insertions, 65 deletions
| @@ -55,8 +55,12 @@ The simplest way to use this library is to just call the `clap.parse` function. | |||
| 55 | 55 | ||
| 56 | ```zig | 56 | ```zig |
| 57 | pub fn main() !void { | 57 | pub fn main() !void { |
| 58 | var gpa = std.heap.DebugAllocator(.{}){}; | 58 | var gpa_state = std.heap.DebugAllocator(.{}){}; |
| 59 | defer _ = gpa.deinit(); | 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(); | ||
| 60 | 64 | ||
| 61 | // First we specify what parameters our program can take. | 65 | // First we specify what parameters our program can take. |
| 62 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. | 66 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. |
| @@ -70,14 +74,14 @@ pub fn main() !void { | |||
| 70 | 74 | ||
| 71 | // Initialize our diagnostics, which can be used for reporting useful errors. | 75 | // Initialize our diagnostics, which can be used for reporting useful errors. |
| 72 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 76 | // 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. |
| 74 | var diag = clap.Diagnostic{}; | 78 | var diag = clap.Diagnostic{}; |
| 75 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 79 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 76 | .diagnostic = &diag, | 80 | .diagnostic = &diag, |
| 77 | .allocator = gpa.allocator(), | 81 | .allocator = gpa, |
| 78 | }) catch |err| { | 82 | }) catch |err| { |
| 79 | // Report useful error and exit. | 83 | // Report useful error and exit. |
| 80 | try diag.reportToFile(.stderr(), err); | 84 | try diag.reportToFile(io, .stderr(), err); |
| 81 | return err; | 85 | return err; |
| 82 | }; | 86 | }; |
| 83 | defer res.deinit(); | 87 | defer res.deinit(); |
| @@ -109,8 +113,12 @@ if you want some other mapping. | |||
| 109 | 113 | ||
| 110 | ```zig | 114 | ```zig |
| 111 | pub fn main() !void { | 115 | pub fn main() !void { |
| 112 | var gpa = std.heap.DebugAllocator(.{}){}; | 116 | var gpa_state = std.heap.DebugAllocator(.{}){}; |
| 113 | defer _ = gpa.deinit(); | 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(); | ||
| 114 | 122 | ||
| 115 | // First we specify what parameters our program can take. | 123 | // First we specify what parameters our program can take. |
| 116 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. | 124 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. |
| @@ -136,12 +144,13 @@ pub fn main() !void { | |||
| 136 | var diag = clap.Diagnostic{}; | 144 | var diag = clap.Diagnostic{}; |
| 137 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ | 145 | var res = clap.parse(clap.Help, ¶ms, parsers, .{ |
| 138 | .diagnostic = &diag, | 146 | .diagnostic = &diag, |
| 139 | .allocator = gpa.allocator(), | 147 | .allocator = gpa, |
| 140 | // The assignment separator can be configured. `--number=1` and `--number:1` is now | 148 | // The assignment separator can be configured. `--number=1` and `--number:1` is now |
| 141 | // allowed. | 149 | // allowed. |
| 142 | .assignment_separators = "=:", | 150 | .assignment_separators = "=:", |
| 143 | }) catch |err| { | 151 | }) catch |err| { |
| 144 | try diag.reportToFile(.stderr(), err); | 152 | // Report useful error and exit. |
| 153 | try diag.reportToFile(io, .stderr(), err); | ||
| 145 | return err; | 154 | return err; |
| 146 | }; | 155 | }; |
| 147 | defer res.deinit(); | 156 | defer res.deinit(); |
| @@ -194,6 +203,9 @@ pub fn main() !void { | |||
| 194 | const gpa = gpa_state.allocator(); | 203 | const gpa = gpa_state.allocator(); |
| 195 | defer _ = gpa_state.deinit(); | 204 | defer _ = gpa_state.deinit(); |
| 196 | 205 | ||
| 206 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 207 | const io: std.Io = threaded.io(); | ||
| 208 | |||
| 197 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); | 209 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); |
| 198 | defer iter.deinit(); | 210 | defer iter.deinit(); |
| 199 | 211 | ||
| @@ -211,7 +223,7 @@ pub fn main() !void { | |||
| 211 | // not fully consumed. It can then be reused to parse the arguments for subcommands. | 223 | // not fully consumed. It can then be reused to parse the arguments for subcommands. |
| 212 | .terminating_positional = 0, | 224 | .terminating_positional = 0, |
| 213 | }) catch |err| { | 225 | }) catch |err| { |
| 214 | try diag.reportToFile(.stderr(), err); | 226 | try diag.reportToFile(io, .stderr(), err); |
| 215 | return err; | 227 | return err; |
| 216 | }; | 228 | }; |
| 217 | defer res.deinit(); | 229 | defer res.deinit(); |
| @@ -222,11 +234,11 @@ pub fn main() !void { | |||
| 222 | const command = res.positionals[0] orelse return error.MissingCommand; | 234 | const command = res.positionals[0] orelse return error.MissingCommand; |
| 223 | switch (command) { | 235 | switch (command) { |
| 224 | .help => std.debug.print("--help\n", .{}), | 236 | .help => std.debug.print("--help\n", .{}), |
| 225 | .math => try mathMain(gpa, &iter, res), | 237 | .math => try mathMain(io, gpa, &iter, res), |
| 226 | } | 238 | } |
| 227 | } | 239 | } |
| 228 | 240 | ||
| 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 { |
| 230 | // The parent arguments are not used here, but there are cases where it might be useful, so | 242 | // The parent arguments are not used here, but there are cases where it might be useful, so |
| 231 | // this example shows how to pass the arguments around. | 243 | // this example shows how to pass the arguments around. |
| 232 | _ = main_args; | 244 | _ = main_args; |
| @@ -247,7 +259,7 @@ fn mathMain(gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: M | |||
| 247 | .diagnostic = &diag, | 259 | .diagnostic = &diag, |
| 248 | .allocator = gpa, | 260 | .allocator = gpa, |
| 249 | }) catch |err| { | 261 | }) catch |err| { |
| 250 | try diag.reportToFile(.stderr(), err); | 262 | try diag.reportToFile(io, .stderr(), err); |
| 251 | return err; // propagate error | 263 | return err; // propagate error |
| 252 | }; | 264 | }; |
| 253 | defer res.deinit(); | 265 | defer res.deinit(); |
| @@ -273,7 +285,12 @@ The `streaming.Clap` is the base of all the other parsers. It's a streaming pars | |||
| 273 | 285 | ||
| 274 | ```zig | 286 | ```zig |
| 275 | pub fn main() !void { | 287 | 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(); | ||
| 277 | 294 | ||
| 278 | // First we specify what parameters our program can take. | 295 | // First we specify what parameters our program can take. |
| 279 | const params = [_]clap.Param(u8){ | 296 | const params = [_]clap.Param(u8){ |
| @@ -289,7 +306,7 @@ pub fn main() !void { | |||
| 289 | .{ .id = 'f', .takes_value = .one }, | 306 | .{ .id = 'f', .takes_value = .one }, |
| 290 | }; | 307 | }; |
| 291 | 308 | ||
| 292 | var iter = try std.process.ArgIterator.initWithAllocator(allocator); | 309 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); |
| 293 | defer iter.deinit(); | 310 | defer iter.deinit(); |
| 294 | 311 | ||
| 295 | // Skip exe argument. | 312 | // Skip exe argument. |
| @@ -308,7 +325,7 @@ pub fn main() !void { | |||
| 308 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 325 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 309 | while (parser.next() catch |err| { | 326 | while (parser.next() catch |err| { |
| 310 | // Report useful error and exit. | 327 | // Report useful error and exit. |
| 311 | try diag.reportToFile(.stderr(), err); | 328 | try diag.reportToFile(io, .stderr(), err); |
| 312 | return err; | 329 | return err; |
| 313 | }) |arg| { | 330 | }) |arg| { |
| 314 | // arg.param will point to the parameter which matched the argument. | 331 | // arg.param will point to the parameter which matched the argument. |
| @@ -347,8 +364,12 @@ is passed to `help` to control how the help message is printed. | |||
| 347 | 364 | ||
| 348 | ```zig | 365 | ```zig |
| 349 | pub fn main() !void { | 366 | pub fn main() !void { |
| 350 | var gpa = std.heap.DebugAllocator(.{}){}; | 367 | var gpa_state = std.heap.DebugAllocator(.{}){}; |
| 351 | defer _ = gpa.deinit(); | 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(); | ||
| 352 | 373 | ||
| 353 | const params = comptime clap.parseParamsComptime( | 374 | const params = comptime clap.parseParamsComptime( |
| 354 | \\-h, --help Display this help and exit. | 375 | \\-h, --help Display this help and exit. |
| @@ -356,9 +377,7 @@ pub fn main() !void { | |||
| 356 | \\ | 377 | \\ |
| 357 | ); | 378 | ); |
| 358 | 379 | ||
| 359 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 380 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); |
| 360 | .allocator = gpa.allocator(), | ||
| 361 | }); | ||
| 362 | defer res.deinit(); | 381 | defer res.deinit(); |
| 363 | 382 | ||
| 364 | // `clap.help` is a function that can print a simple help message. It can print any `Param` | 383 | // `clap.help` is a function that can print a simple help message. It can print any `Param` |
| @@ -366,7 +385,7 @@ pub fn main() !void { | |||
| 366 | // The last argument contains options as to how `help` should print those parameters. Using | 385 | // The last argument contains options as to how `help` should print those parameters. Using |
| 367 | // `.{}` means the default options. | 386 | // `.{}` means the default options. |
| 368 | if (res.args.help != 0) | 387 | if (res.args.help != 0) |
| 369 | return clap.helpToFile(.stderr(), clap.Help, ¶ms, .{}); | 388 | return clap.helpToFile(io, .stderr(), clap.Help, ¶ms, .{}); |
| 370 | } | 389 | } |
| 371 | 390 | ||
| 372 | const clap = @import("clap"); | 391 | const clap = @import("clap"); |
| @@ -389,8 +408,12 @@ $ zig-out/bin/help --help | |||
| 389 | 408 | ||
| 390 | ```zig | 409 | ```zig |
| 391 | pub fn main() !void { | 410 | pub fn main() !void { |
| 392 | var gpa = std.heap.DebugAllocator(.{}){}; | 411 | var gpa_state = std.heap.DebugAllocator(.{}){}; |
| 393 | defer _ = gpa.deinit(); | 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(); | ||
| 394 | 417 | ||
| 395 | const params = comptime clap.parseParamsComptime( | 418 | const params = comptime clap.parseParamsComptime( |
| 396 | \\-h, --help Display this help and exit. | 419 | \\-h, --help Display this help and exit. |
| @@ -399,15 +422,13 @@ pub fn main() !void { | |||
| 399 | \\ | 422 | \\ |
| 400 | ); | 423 | ); |
| 401 | 424 | ||
| 402 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ | 425 | var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ .allocator = gpa }); |
| 403 | .allocator = gpa.allocator(), | ||
| 404 | }); | ||
| 405 | defer res.deinit(); | 426 | defer res.deinit(); |
| 406 | 427 | ||
| 407 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any | 428 | // `clap.usageToFile` is a function that can print a simple usage string. It can print any |
| 408 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). | 429 | // `Param` where `Id` has a `value` method (`Param(Help)` is one such parameter). |
| 409 | if (res.args.help != 0) | 430 | if (res.args.help != 0) |
| 410 | return clap.usageToFile(.stdout(), clap.Help, ¶ms); | 431 | return clap.usageToFile(io, .stdout(), clap.Help, ¶ms); |
| 411 | } | 432 | } |
| 412 | 433 | ||
| 413 | const clap = @import("clap"); | 434 | const clap = @import("clap"); |
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 @@ | |||
| 1 | pub fn main() !void { | 1 | pub 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, ¶ms, clap.parsers.default, .{ | 15 | var res = try clap.parse(clap.Help, ¶ms, 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 @@ | |||
| 1 | pub fn main() !void { | 1 | pub 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, ¶ms, parsers, .{ | 31 | var res = clap.parse(clap.Help, ¶ms, 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 @@ | |||
| 1 | pub fn main() !void { | 1 | pub 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, ¶ms, clap.parsers.default, .{ | 23 | var res = clap.parse(clap.Help, ¶ms, 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 @@ | |||
| 1 | pub fn main() !void { | 1 | 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(); | ||
| 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 | ||
| 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 { |
| 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, ¶ms, clap.parsers.default, iter, .{ | 79 | var res = clap.parseEx(clap.Help, ¶ms, 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 @@ | |||
| 1 | pub fn main() !void { | 1 | pub 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, ¶ms, clap.parsers.default, .{ | 16 | var res = try clap.parse(clap.Help, ¶ms, 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) |