accept additional positional args to forward

This commit is contained in:
Daylin Morgan 2024-04-11 13:26:43 -05:00
parent bf192f478d
commit a7302bd609
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 38 additions and 7 deletions

View file

@ -3,9 +3,12 @@ const std = @import("std");
const yazap = @import("yazap"); const yazap = @import("yazap");
const App = yazap.App; const App = yazap.App;
const Arg = yazap.Arg; const Arg = yazap.Arg;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
allocator: Allocator,
app: App, app: App,
forward: ?[][]const u8 = null,
matches: *const yazap.ArgMatches = undefined,
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");
@ -27,16 +30,41 @@ pub fn init(allocator: Allocator) !Cli {
&cmd_switch, &cmd_switch,
&cmd_boot, &cmd_boot,
}) |subcmd| { }) |subcmd| {
try subcmd.addArg(Arg.positional("forward", null, null));
try subcmd.addArg(Arg.singleValueOption("flake", 'f', "path to flake")); try subcmd.addArg(Arg.singleValueOption("flake", 'f', "path to flake"));
try subcmd.addArg(Arg.singleValueOption("host", null, "hostname (default: current host)")); try subcmd.addArg(Arg.singleValueOption("host", null, "hostname (default: current host)"));
try subcmd.addArg(Arg.booleanOption("no-pinix", null, "don't use pinix")); try subcmd.addArg(Arg.booleanOption("no-pinix", null, "don't use pinix"));
try oizys.addSubcommand(subcmd.*); try oizys.addSubcommand(subcmd.*);
} }
// TODO: accept positinal args after -- to forward along? return Cli{
return Cli{ .app = app }; .allocator = allocator,
.app = app,
};
}
pub fn parse(self: *Cli) !void {
const args = try std.process.argsAlloc(self.allocator);
defer std.process.argsFree(self.allocator, args);
var forward = std.ArrayList([]const u8).init(self.allocator);
// 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;
};
self.forward = try forward.toOwnedSlice();
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(); self.app.deinit();
for (self.forward) |arg| self.allocator.free(arg);
self.allocator.free(self.forward);
} }

View file

@ -11,6 +11,7 @@ output: []const u8,
cmd: OizysCmd, cmd: OizysCmd,
no_pinix: bool, no_pinix: bool,
debug: bool = false, debug: bool = false,
forward: ?[][]const u8,
pub const OizysCmd = enum { pub const OizysCmd = enum {
dry, dry,
@ -21,7 +22,7 @@ pub const OizysCmd = enum {
build, build,
}; };
pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches) !Oizys { pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches, forward: ?[][]const u8) !Oizys {
const cmd = matches.subcommand.?.name; const cmd = matches.subcommand.?.name;
const flags = matches.subcommandMatches(cmd).?; const flags = matches.subcommandMatches(cmd).?;
const host = flags.getSingleValue("host") orelse const host = flags.getSingleValue("host") orelse
@ -41,6 +42,7 @@ pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches) !Oizys {
.cmd = std.meta.stringToEnum(OizysCmd, cmd).?, .cmd = std.meta.stringToEnum(OizysCmd, cmd).?,
.cache_name = flags.getSingleValue("cache") orelse "daylin", .cache_name = flags.getSingleValue("cache") orelse "daylin",
.no_pinix = flags.containsArg("no-pinix"), .no_pinix = flags.containsArg("no-pinix"),
.forward = forward,
}; };
} }
@ -91,6 +93,7 @@ pub fn runNixCmd(self: *Oizys, cmd: NixCmd, argv: []const []const u8) !void {
NixCmd.NixosRebuild => try args.appendSlice(&.{ "sudo", self.nixos_rebuild() }), NixCmd.NixosRebuild => try args.appendSlice(&.{ "sudo", self.nixos_rebuild() }),
} }
try args.appendSlice(argv); try args.appendSlice(argv);
if (self.forward) |fwd| try args.appendSlice(fwd);
var p = std.ChildProcess.init(args.items, self.allocator); var p = std.ChildProcess.init(args.items, self.allocator);
_ = try p.spawnAndWait(); _ = try p.spawnAndWait();
} }

View file

@ -9,13 +9,13 @@ pub fn main() !void {
const allocator = arena.allocator(); const allocator = arena.allocator();
var cli = try Cli.init(allocator); var cli = try Cli.init(allocator);
const matches = try cli.app.parseProcess(); try cli.parse();
if (!matches.containsArgs()) { if (!cli.matches.containsArgs()) {
try cli.app.displayHelp(); try cli.app.displayHelp();
return; return;
} }
var oizys = try Oizys.init(allocator, matches); var oizys = try Oizys.init(allocator, cli.matches, cli.forward);
try oizys.run(); try oizys.run();
} }