allocator interface changes

This commit is contained in:
peterino2 2026-02-09 18:50:28 -08:00
parent 65e4801dad
commit d980c5bfe9
4 changed files with 36 additions and 21 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.zig-cache
.zig-out
zig-out/

View File

@ -41,16 +41,19 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
zargs.setAllocator(allocator);
defer zargs.shutdown();
zargs.setAllocator(allocator);
// Use populate to register metadata and parse arguments
const config = try zargs.parse(Config, allocator);
const config = try zargs.parse(Config);
// Check if help was requested after parsing
if (zargs.isHelp(allocator)) {
if (zargs.isHelp()) {
// Generate and display help using the registry
const help_text = try zargs.getUsageAlloc(allocator, "file_processor");
defer allocator.free(help_text);
const help_text = try zargs.getUsageAlloc("file_processor");
defer zargs.getAllocator().free(help_text);
std.debug.print("{s}", .{help_text});
return;

View File

@ -40,18 +40,18 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
defer zargs.shutdown();
zargs.setAllocator(allocator);
// Lazy populate: metadata registration and parsing happen on-demand
const graphics = try zargs.parse(GraphicsConfig, allocator);
const audio = try zargs.parse(AudioConfig, allocator);
const engine = try zargs.parse(EngineConfig, allocator);
const graphics = try zargs.parse(GraphicsConfig);
const audio = try zargs.parse(AudioConfig);
const engine = try zargs.parse(EngineConfig);
// Check for help after all modules are populated
if (zargs.isHelp(allocator)) {
if (zargs.isHelp()) {
const program_name = "multi-module";
const help_text = try zargs.getUsageAlloc(allocator, program_name);
const help_text = try zargs.getUsageAlloc(program_name);
defer allocator.free(help_text);
try std.fs.File.stdout().writeAll(help_text);
return;

View File

@ -11,26 +11,37 @@ pub const generateHelpText = @import("help.zig").generateHelpText;
pub var gRegistry: ?ArgumentRegistry = null;
pub fn getUsageAlloc(allocator: std.mem.Allocator, programName: []const u8) ![]const u8 {
pub var gAllocator: std.mem.Allocator = undefined;
// an allocator must be set before anything in zargs can be used
pub fn setAllocator(allocator: std.mem.Allocator) void {
gAllocator = allocator;
}
pub fn getAllocator() std.mem.Allocator {
return gAllocator;
}
pub fn getUsageAlloc(programName: []const u8) ![]const u8 {
if (gRegistry == null) {
gRegistry = ArgumentRegistry.init(allocator);
gRegistry = ArgumentRegistry.init(gAllocator);
}
return try generateHelpText(gRegistry.?, allocator, programName);
return try generateHelpText(gRegistry.?, gAllocator, programName);
}
pub fn parse(comptime T: type, allocator: std.mem.Allocator) !T {
pub fn parse(comptime T: type) !T {
if (gRegistry == null) {
gRegistry = ArgumentRegistry.init(allocator);
gRegistry = ArgumentRegistry.init(gAllocator);
}
const value = try gRegistry.?.populate(T, @typeName(T), allocator);
const value = try gRegistry.?.populate(T, @typeName(T), gAllocator);
return value;
}
pub fn isHelp(allocator: std.mem.Allocator) bool {
pub fn isHelp() bool {
if (gRegistry == null) {
gRegistry = ArgumentRegistry.init(allocator);
gRegistry = ArgumentRegistry.init(gAllocator);
}
gRegistry.?.scanForHelp();
return gRegistry.?.help_requested;