zig-args/demo.zig
LordMZTE 50e2ba9a3b update to new zig module system
This allows using zig-args with the new Zig package manager.
2023-02-06 19:55:06 +01:00

45 lines
1.3 KiB
Zig

const std = @import("std");
const argsParser = @import("args");
pub fn main() !u8 {
var argsAllocator = std.heap.page_allocator;
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});
}
return 0;
}