add some pointless defers

This commit is contained in:
Daylin Morgan 2024-04-11 15:22:49 -05:00
parent 7dfe185a9f
commit 69ac3fa6a6
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 21 additions and 16 deletions

View file

@ -7,8 +7,9 @@ const Allocator = std.mem.Allocator;
allocator: Allocator, allocator: Allocator,
app: App, app: App,
forward: ?[][]const u8 = null,
matches: *const yazap.ArgMatches = undefined, matches: *const yazap.ArgMatches = undefined,
forward: ?[][]const u8 = null,
process_args: ?[]const [:0]u8 = null,
pub fn init(allocator: Allocator) !Cli { pub fn init(allocator: Allocator) !Cli {
var app = App.init(allocator, "oizys", "nix begat oizys"); var app = App.init(allocator, "oizys", "nix begat oizys");
@ -43,31 +44,32 @@ pub fn init(allocator: Allocator) !Cli {
}; };
} }
fn get_forward_args(self: *Cli, args: [][]const u8) !usize { fn get_forward_args(self: *Cli, args: []const [:0]u8) !usize {
var forward = std.ArrayList([]const u8).init(self.allocator); var forward = std.ArrayList([]const u8).init(self.allocator);
var delim_idx: usize = args.len;
for (args, 0..) |arg, i| const delim_idx: usize = delim_lookup: for (args, 0..) |arg, i| {
if (std.mem.eql(u8, "--", arg)) { if (std.mem.eql(u8, "--", arg)) break :delim_lookup i;
for (args[i + 1 ..]) |fwd| } else args.len;
try forward.append(try self.allocator.dupe(u8, fwd));
delim_idx = i; if (args.len > delim_idx)
break; for (args[delim_idx + 1 ..]) |fwd|
}; try forward.append(fwd);
self.forward = try forward.toOwnedSlice(); self.forward = try forward.toOwnedSlice();
return delim_idx; return delim_idx;
} }
pub fn parse(self: *Cli) !void { pub fn parse(self: *Cli) !void {
const args = try std.process.argsAlloc(self.allocator); self.process_args = try std.process.argsAlloc(self.allocator);
defer std.process.argsFree(self.allocator, args); const delim_idx = try self.get_forward_args(self.process_args.?);
const delim_idx = try self.get_forward_args(args); self.matches = try self.app.parseFrom(self.process_args.?[1..delim_idx]);
self.matches = try self.app.parseFrom(args[1..delim_idx]);
} }
pub fn deinit(self: *Cli) void { pub fn deinit(self: *Cli) void {
self.app.deinit(); std.process.argsFree(self.allocator, self.process_args.?);
for (self.forward) |arg| self.allocator.free(arg); if (self.forward) |fwd| {
self.allocator.free(self.forward); for (fwd) |arg| self.allocator.free(arg);
self.allocator.free(fwd);
}
self.app.deinit();
} }

View file

@ -10,6 +10,7 @@ pub fn main() !void {
var cli = try Cli.init(allocator); var cli = try Cli.init(allocator);
try cli.parse(); try cli.parse();
defer cli.deinit();
if (!cli.matches.containsArgs()) { if (!cli.matches.containsArgs()) {
try cli.app.displayHelp(); try cli.app.displayHelp();
@ -17,5 +18,7 @@ pub fn main() !void {
} }
var oizys = try Oizys.init(allocator, cli.matches, cli.forward); var oizys = try Oizys.init(allocator, cli.matches, cli.forward);
defer oizys.deinit();
try oizys.run(); try oizys.run();
} }