|
1 | 1 | const std = @import("std"); |
2 | 2 | const cli = @import("cli"); |
3 | 3 |
|
4 | | -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
5 | | -const allocator = gpa.allocator(); |
6 | | - |
7 | 4 | var config = struct { |
8 | 5 | ip: []const u8 = undefined, |
9 | 6 | int: i32 = undefined, |
@@ -52,9 +49,9 @@ fn sub3(r: *cli.AppRunner) !cli.Command { |
52 | 49 | }; |
53 | 50 | } |
54 | 51 |
|
55 | | -fn parseArgs(io: std.Io) cli.AppRunner.Error!cli.ExecFn { |
| 52 | +fn parseArgs(init: *const std.process.Init) cli.AppRunner.Error!cli.ExecFn { |
56 | 53 | // This allocator will be used to allocate config.ip and config.arg2. |
57 | | - var r = try cli.AppRunner.init(io, allocator); |
| 54 | + var r = cli.AppRunner.init(init); |
58 | 55 |
|
59 | 56 | const sub2 = cli.Command{ |
60 | 57 | .name = "sub2", |
@@ -129,30 +126,9 @@ fn parseArgs(io: std.Io) cli.AppRunner.Error!cli.ExecFn { |
129 | 126 | return r.getAction(&app); |
130 | 127 | } |
131 | 128 |
|
132 | | -pub fn main() anyerror!void { |
133 | | - var threaded = std.Io.Threaded.init(std.heap.page_allocator, .{}); |
134 | | - defer threaded.deinit(); |
135 | | - |
136 | | - const action = try parseArgs(threaded.io()); |
137 | | - const r = action(); |
138 | | - freeConfig(); |
139 | | - return r; |
140 | | -} |
141 | | - |
142 | | -// Usually, you just use an arena allocator to free all allocated resources in a batch. |
143 | | -// This only illustrates the fact that the config data are allocated with the allocator |
144 | | -// you pass to cli.AppRunner.init(allocator). |
145 | | -fn freeConfig() void { |
146 | | - allocator.free(config.ip); |
147 | | - if (config.arg2.len > 0) { |
148 | | - for (config.arg2) |item| { |
149 | | - allocator.free(item); |
150 | | - } |
151 | | - allocator.free(config.arg2); |
152 | | - } |
153 | | - if (gpa.deinit() == .leak) { |
154 | | - @panic("config leaked"); |
155 | | - } |
| 129 | +pub fn main(init: std.process.Init) anyerror!void { |
| 130 | + const action = try parseArgs(&init); |
| 131 | + return action(); |
156 | 132 | } |
157 | 133 |
|
158 | 134 | fn run_sub3() !void { |
|
0 commit comments