Simple-to-use argument parser with struct-based config
-- is assumed to be a positional argument- is interpreted as a positional argument which can be used as the stdin/stdout file placeholder-dfe--option=value or --option value syntax (use --option=-- if you need -- as a long option argument)demo_verb.zig)yes, true, y, no, false, n is interpreted)Add the dependency in your build.zig.zon by running the following command:
zig fetch --save=args git+https://github.com/ikskuh/zig-args#master
Add it to your exe in build.zig:
exe.root_module.addImport("args", b.dependency("args", .{ .target = target, .optimize = optimize }).module("args"));
Then you can import it from your code:
const argsParser = @import("args");
const options = argsParser.parseForCurrentProcess(struct {
// This declares long options for double hyphen
output: ?[]const u8 = null,
@"with-offset": bool = false,
@"with-hexdump": bool = false,
@"intermix-source": bool = false,
numberOfBytes: ?i32 = null,
signed_number: ?i64 = null,
unsigned_number: ?u64 = null,
mode: enum { default, special, slow, fast } = .default,
// This declares short-hand options for single hyphen
pub const shorthands = .{
.S = "intermix-source",
.b = "with-hexdump",
.O = "with-offset",
.o = "output",
};
}, argsAllocator, .print) catch return 1;
defer options.deinit();
std.debug.print("executable name: {?s}\n", .{options.executable_name});
std.debug.print("parsed options:\n", .{});
inline for (std.meta.fields(@TypeOf(options.options))) |fld| {
std.debug.print("\t{s} = {any}\n", .{
fld.name,
@field(options.options, fld.name),
});
}
std.debug.print("parsed positionals:\n", .{});
for (options.positionals) |arg| {
std.debug.print("\t'{s}'\n", .{arg});
}