diff --git a/.gitignore b/.gitignore index 25320bc..5dfd150 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + .zig-cache -.zig-out +zig-out/ diff --git a/examples/file_processor.zig b/examples/file_processor.zig index 8811a5e..f40b5ca 100644 --- a/examples/file_processor.zig +++ b/examples/file_processor.zig @@ -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; diff --git a/examples/multi_module.zig b/examples/multi_module.zig index 5faa8bf..08cb0a4 100644 --- a/examples/multi_module.zig +++ b/examples/multi_module.zig @@ -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; diff --git a/src/main.zig b/src/main.zig index bc4074c..3f35f74 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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 { - if (gRegistry == null) { - gRegistry = ArgumentRegistry.init(allocator); - } +pub var gAllocator: std.mem.Allocator = undefined; - return try generateHelpText(gRegistry.?, allocator, programName); +// an allocator must be set before anything in zargs can be used +pub fn setAllocator(allocator: std.mem.Allocator) void { + gAllocator = allocator; } -pub fn parse(comptime T: type, allocator: std.mem.Allocator) !T { +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); } - const value = try gRegistry.?.populate(T, @typeName(T), allocator); + return try generateHelpText(gRegistry.?, gAllocator, programName); +} + +pub fn parse(comptime T: type) !T { + if (gRegistry == null) { + gRegistry = ArgumentRegistry.init(gAllocator); + } + + 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;