zargs/examples/multi_module.zig

80 lines
2.5 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, err } = .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();
defer zargs.shutdown();
// 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);
// Check for help after all modules are populated
if (zargs.isHelp(allocator)) {
const program_name = "multi-module";
const help_text = try zargs.getUsageAlloc(allocator, program_name);
defer allocator.free(help_text);
try std.fs.File.stdout().writeAll(help_text);
return;
}
// Use the configurations
std.debug.print("=== Game Engine Starting ===\n\n", .{});
std.debug.print("Graphics:\n", .{});
std.debug.print(" Resolution: {s}\n", .{graphics.resolution});
std.debug.print(" Fullscreen: {}\n", .{graphics.fullscreen});
std.debug.print(" VSync: {}\n\n", .{graphics.vsync});
std.debug.print("Audio:\n", .{});
std.debug.print(" Volume: {d}%\n", .{audio.volume});
std.debug.print(" Muted: {}\n\n", .{audio.muted});
std.debug.print("Engine:\n", .{});
std.debug.print(" Log Level: {s}\n", .{@tagName(engine.log_level)});
if (engine.config_file) |file| {
std.debug.print(" Config File: {s}\n", .{file});
}
std.debug.print("\n[Engine initialized successfully]\n", .{});
}