Backlog/lib/zargs/examples/simple.zig

64 lines
1.7 KiB
Zig

const std = @import("std");
const zargs = @import("zargs");
// Define your configuration struct
const Config = struct {
verbose: bool = false,
output: []const u8 = "output.txt",
count: u32 = 10,
mode: enum { fast, slow, balanced } = .balanced,
// Add metadata for each field
pub const meta = .{
.verbose = .{
.short = 'v',
.help = "Enable verbose output",
},
.output = .{
.short = 'o',
.help = "Output file path",
},
.count = .{
.short = 'c',
.help = "Number of items to process",
},
.mode = .{
.short = 'm',
.help = "Processing mode",
},
};
};
pub fn main() !void {
// Setup allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Get command-line arguments
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
// Parse arguments into Config struct
const config = zargs.parse(Config, allocator, args) catch |err| {
if (err == error.HelpRequested) {
// Help was shown, exit gracefully
return;
}
return err;
};
// Use the configuration
const stdout = std.io.getStdOut().writer();
if (config.verbose) {
try stdout.print("Verbose mode enabled\n", .{});
}
try stdout.print("Output file: {s}\n", .{config.output});
try stdout.print("Processing {d} items in {s} mode\n", .{ config.count, @tagName(config.mode) });
// Your application logic here
try stdout.print("\nProcessing...\n", .{});
}