use seperate funtion to get forwarded args

This commit is contained in:
Daylin Morgan 2024-04-11 13:57:58 -05:00
parent a7302bd609
commit 7dfe185a9f
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -43,23 +43,26 @@ pub fn init(allocator: Allocator) !Cli {
};
}
pub fn parse(self: *Cli) !void {
const args = try std.process.argsAlloc(self.allocator);
defer std.process.argsFree(self.allocator, args);
fn get_forward_args(self: *Cli, args: [][]const u8) !usize {
var forward = std.ArrayList([]const u8).init(self.allocator);
var delim_idx: usize = args.len;
// TODO: simplify, this smells
const delim_idx = blk: {
for (args, 0..) |arg, i|
if (std.mem.eql(u8, "--", arg)) {
for (args[i + 1 ..]) |fwd|
try forward.append(try self.allocator.dupe(u8, fwd));
break :blk i;
};
break :blk args.len;
delim_idx = i;
break;
};
self.forward = try forward.toOwnedSlice();
return delim_idx;
}
pub fn parse(self: *Cli) !void {
const args = try std.process.argsAlloc(self.allocator);
defer std.process.argsFree(self.allocator, args);
const delim_idx = try self.get_forward_args(args);
self.matches = try self.app.parseFrom(args[1..delim_idx]);
}