From 76e8dedf145f53ac89d01a74759c89b9d7e92712 Mon Sep 17 00:00:00 2001 From: torque Date: Sun, 10 Sep 2023 14:52:49 -0700 Subject: [PATCH] command: add `commandGroup` function This just creates an empty command with an auto-assigned noop callback. This is useful sugar for creating a group of commands under a common name because previously the user would have to define their own noop callback and bind it. This just takes a description string (and, optionally, a help flag override). --- source/command.zig | 21 +++++++++++++++++++++ source/noclip.zig | 1 + 2 files changed, 22 insertions(+) diff --git a/source/command.zig b/source/command.zig index e6b3eb4..d0174a9 100644 --- a/source/command.zig +++ b/source/command.zig @@ -72,6 +72,21 @@ fn BuilderGenerics(comptime UserContext: type) type { }; } +pub const GroupOptions = struct { + help_flag: ShortLongPair = .{ .short_tag = "-h", .long_tag = "--help" }, + description: []const u8, +}; + +pub fn commandGroup(allocator: std.mem.Allocator, comptime options: GroupOptions) !ParserInterface { + const cmd = comptime CommandBuilder(void){ + .help_flag = options.help_flag, + .description = options.description, + .subcommand_required = true, + }; + + return try cmd.createInterface(allocator, cmd.noopCallback()); +} + fn InterfaceCreator(comptime Command: type) type { return if (Command.ICC.InputType()) |Type| struct { @@ -329,6 +344,12 @@ pub fn CommandBuilder(comptime UserContext: type) type { return self.param_spec.realTuple(); } + pub fn noopCallback(comptime self: @This()) self.CallbackSignature() { + return struct { + fn callback(_: UserContextType, _: self.Output()) !void {} + }.callback; + } + pub fn CallbackSignature(comptime self: @This()) type { return *const fn (UserContextType, self.Output()) anyerror!void; } diff --git a/source/noclip.zig b/source/noclip.zig index 72319e0..1e810f7 100644 --- a/source/noclip.zig +++ b/source/noclip.zig @@ -7,4 +7,5 @@ pub const parameters = @import("./parameters.zig"); pub const parser = @import("./parser.zig"); pub const CommandBuilder = command.CommandBuilder; +pub const commandGroup = command.commandGroup; pub const ParserInterface = parser.ParserInterface;