mirror of
https://github.com/daylinmorgan/oizys.git
synced 2025-01-09 22:47:33 -06:00
add some pointless defers
This commit is contained in:
parent
7dfe185a9f
commit
69ac3fa6a6
2 changed files with 21 additions and 16 deletions
|
@ -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 {
|
||||||
|
std.process.argsFree(self.allocator, self.process_args.?);
|
||||||
|
if (self.forward) |fwd| {
|
||||||
|
for (fwd) |arg| self.allocator.free(arg);
|
||||||
|
self.allocator.free(fwd);
|
||||||
|
}
|
||||||
self.app.deinit();
|
self.app.deinit();
|
||||||
for (self.forward) |arg| self.allocator.free(arg);
|
|
||||||
self.allocator.free(self.forward);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue