Backlog/lib/zargs/examples/multi_module.zig

95 lines
3.2 KiB
Zig

const std = @import("std");
const zargs = @import("zargs");
// Graphics module configuration
const GraphicsConfig = struct {
resolution: []const u8 = "1920x1080",
fullscreen: bool = false,
vsync: bool = true,
pub const meta = .{
.resolution = .{ .short = 'r', .help = "Screen resolution" },
.fullscreen = .{ .short = 'f', .help = "Enable fullscreen mode" },
.vsync = .{ .help = "Enable vertical sync" },
};
};
// Audio module configuration
const AudioConfig = struct {
volume: u32 = 80,
muted: bool = false,
pub const meta = .{
.volume = .{ .help = "Master volume (0-100)" },
.muted = .{ .short = 'm', .help = "Start with audio muted" },
};
};
// Engine configuration
const EngineConfig = struct {
log_level: enum { debug, info, warn, error } = .info,
config_file: ?[]const u8 = null,
pub const meta = .{
.log_level = .{ .help = "Logging level" },
.config_file = .{ .short = 'c', .help = "Load configuration from file" },
};
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
// Create a shared registry for multiple modules
var registry = zargs.ArgumentRegistry.init(allocator);
defer registry.deinit();
// Register all module configurations
try registry.registerMetadata(GraphicsConfig, "Graphics");
try registry.registerMetadata(AudioConfig, "Audio");
try registry.registerMetadata(EngineConfig, "Engine");
// Parse arguments
try zargs.parseArgv(&registry, args);
// Check for help
if (registry.isHelpRequested()) {
const program_name = if (args.len > 0) args[0] else null;
const help_text = try zargs.generateHelpText(&registry, allocator, program_name);
defer allocator.free(help_text);
try std.io.getStdOut().writeAll(help_text);
return;
}
// Populate each module's configuration
const graphics = try zargs.populateStruct(GraphicsConfig, &registry, allocator);
const audio = try zargs.populateStruct(AudioConfig, &registry, allocator);
const engine = try zargs.populateStruct(EngineConfig, &registry, allocator);
// Use the configurations
const stdout = std.io.getStdOut().writer();
try stdout.print("=== Game Engine Starting ===\n\n", .{});
try stdout.print("Graphics:\n", .{});
try stdout.print(" Resolution: {s}\n", .{graphics.resolution});
try stdout.print(" Fullscreen: {}\n", .{graphics.fullscreen});
try stdout.print(" VSync: {}\n\n", .{graphics.vsync});
try stdout.print("Audio:\n", .{});
try stdout.print(" Volume: {d}%\n", .{audio.volume});
try stdout.print(" Muted: {}\n\n", .{audio.muted});
try stdout.print("Engine:\n", .{});
try stdout.print(" Log Level: {s}\n", .{@tagName(engine.log_level)});
if (engine.config_file) |file| {
try stdout.print(" Config File: {s}\n", .{file});
}
try stdout.print("\n[Engine initialized successfully]\n", .{});
}