diff options
| author | 2026-01-25 09:27:04 +1100 | |
|---|---|---|
| committer | 2026-01-24 23:27:04 +0100 | |
| commit | 1d3d273524e3c180f015ff1e93c83075e4634e2c (patch) | |
| tree | 55693ab9cf02ecc4c3f171882259ef41adab4fb7 /example/subcommands.zig | |
| parent | fix: Regression in simple-ex example (diff) | |
| download | zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.gz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.tar.xz zig-clap-1d3d273524e3c180f015ff1e93c83075e4634e2c.zip | |
Diffstat (limited to 'example/subcommands.zig')
| -rw-r--r-- | example/subcommands.zig | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/example/subcommands.zig b/example/subcommands.zig index bd8625b..0b14efe 100644 --- a/example/subcommands.zig +++ b/example/subcommands.zig | |||
| @@ -19,15 +19,8 @@ const main_params = clap.parseParamsComptime( | |||
| 19 | // get the return type of `clap.parse` and `clap.parseEx`. | 19 | // get the return type of `clap.parse` and `clap.parseEx`. |
| 20 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); | 20 | const MainArgs = clap.ResultEx(clap.Help, &main_params, main_parsers); |
| 21 | 21 | ||
| 22 | pub fn main() !void { | 22 | pub fn main(init: std.process.Init) !void { |
| 23 | var gpa_state = std.heap.DebugAllocator(.{}){}; | 23 | var iter = try init.minimal.args.iterateAllocator(init.gpa); |
| 24 | const gpa = gpa_state.allocator(); | ||
| 25 | defer _ = gpa_state.deinit(); | ||
| 26 | |||
| 27 | var threaded: std.Io.Threaded = .init_single_threaded; | ||
| 28 | const io: std.Io = threaded.io(); | ||
| 29 | |||
| 30 | var iter = try std.process.ArgIterator.initWithAllocator(gpa); | ||
| 31 | defer iter.deinit(); | 24 | defer iter.deinit(); |
| 32 | 25 | ||
| 33 | _ = iter.next(); | 26 | _ = iter.next(); |
| @@ -35,7 +28,7 @@ pub fn main() !void { | |||
| 35 | var diag = clap.Diagnostic{}; | 28 | var diag = clap.Diagnostic{}; |
| 36 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ | 29 | var res = clap.parseEx(clap.Help, &main_params, main_parsers, &iter, .{ |
| 37 | .diagnostic = &diag, | 30 | .diagnostic = &diag, |
| 38 | .allocator = gpa, | 31 | .allocator = init.gpa, |
| 39 | 32 | ||
| 40 | // Terminate the parsing of arguments after parsing the first positional (0 is passed | 33 | // Terminate the parsing of arguments after parsing the first positional (0 is passed |
| 41 | // here because parsed positionals are, like slices and arrays, indexed starting at 0). | 34 | // here because parsed positionals are, like slices and arrays, indexed starting at 0). |
| @@ -44,7 +37,7 @@ pub fn main() !void { | |||
| 44 | // not fully consumed. It can then be reused to parse the arguments for subcommands. | 37 | // not fully consumed. It can then be reused to parse the arguments for subcommands. |
| 45 | .terminating_positional = 0, | 38 | .terminating_positional = 0, |
| 46 | }) catch |err| { | 39 | }) catch |err| { |
| 47 | try diag.reportToFile(io, .stderr(), err); | 40 | try diag.reportToFile(init.io, .stderr(), err); |
| 48 | return err; | 41 | return err; |
| 49 | }; | 42 | }; |
| 50 | defer res.deinit(); | 43 | defer res.deinit(); |
| @@ -55,11 +48,11 @@ pub fn main() !void { | |||
| 55 | const command = res.positionals[0] orelse return error.MissingCommand; | 48 | const command = res.positionals[0] orelse return error.MissingCommand; |
| 56 | switch (command) { | 49 | switch (command) { |
| 57 | .help => std.debug.print("--help\n", .{}), | 50 | .help => std.debug.print("--help\n", .{}), |
| 58 | .math => try mathMain(io, gpa, &iter, res), | 51 | .math => try mathMain(init.io, init.gpa, &iter, res), |
| 59 | } | 52 | } |
| 60 | } | 53 | } |
| 61 | 54 | ||
| 62 | fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.ArgIterator, main_args: MainArgs) !void { | 55 | fn mathMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator, main_args: MainArgs) !void { |
| 63 | // The parent arguments are not used here, but there are cases where it might be useful, so | 56 | // 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. | 57 | // this example shows how to pass the arguments around. |
| 65 | _ = main_args; | 58 | _ = main_args; |