noclip: fix program basename getting for subcommands

This doesn't unnecessarily consume arguments from the CLI argument
iterator any more.
This commit is contained in:
torque 2023-03-23 00:43:23 -07:00
parent 5eee6ecde0
commit 261ae7af31
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk

View File

@ -63,8 +63,11 @@ pub fn CommandParser(
// this should be copied at compile time // this should be copied at compile time
var data: params.CommandData = commandData; var data: params.CommandData = commandData;
/// parse command line arguments from an iterator
pub fn execute(self: @This(), alloc: std.mem.Allocator, comptime argit_type: type, argit: *argit_type, context: UserContext) !void { pub fn execute(self: @This(), alloc: std.mem.Allocator, comptime argit_type: type, argit: *argit_type, context: UserContext) !void {
return try self.internal_execute(alloc, argit_type, argit, context, null);
}
fn internal_execute(self: @This(), alloc: std.mem.Allocator, comptime argit_type: type, argit: *argit_type, context: UserContext, prog: ?[]const u8) !void {
try self.attachSubcommands(alloc); try self.attachSubcommands(alloc);
var result: ResultType = createCommandresult(); var result: ResultType = createCommandresult();
@ -74,7 +77,7 @@ pub fn CommandParser(
try extractEnvVars(alloc, &result, &required, context); try extractEnvVars(alloc, &result, &required, context);
// TODO: this does not even slightly work with subcommands // TODO: this does not even slightly work with subcommands
const progName = std.fs.path.basename(argit.next() orelse unreachable); const progName = prog orelse std.fs.path.basename(argit.next() orelse @panic("base, name?"));
// TODO: only do this if the help flag has been passed. Alternatively, try // TODO: only do this if the help flag has been passed. Alternatively, try
// to assemble this at comptime? // to assemble this at comptime?
@ -197,11 +200,15 @@ pub fn CommandParser(
inline for (spec) |param| { inline for (spec) |param| {
switch (@TypeOf(param).brand) { switch (@TypeOf(param).brand) {
.Command => { .Command => {
if (std.mem.eql(u8, @TypeOf(param).data.name, arg)) { const name = @TypeOf(param).data.name;
if (std.mem.eql(u8, name, arg)) {
// we're calling a subcommand // we're calling a subcommand
try checkErrors(seenArgs, required); try checkErrors(seenArgs, required);
try callback(context, result); try callback(context, result);
return param.execute(alloc, argit_type, argit, context);
const combined = try std.mem.join(alloc, " ", &[_][]const u8{ progName, name });
defer alloc.free(combined);
return param.internal_execute(alloc, argit_type, argit, context, combined);
} }
}, },
.Argument => { .Argument => {