zig-args/demo.zig

63 lines
2 KiB
Zig
Raw Normal View History

2020-03-04 22:37:11 +01:00
const std = @import("std");
const argsParser = @import("args");
2020-03-04 22:37:11 +01:00
pub fn main() !u8 {
const argsAllocator = std.heap.page_allocator;
2020-03-04 22:37:11 +01:00
const Options = struct {
2020-03-04 22:38:26 +01:00
// This declares long options for double hyphen
2020-03-04 22:37:11 +01:00
output: ?[]const u8 = null,
@"with-offset": bool = false,
@"with-hexdump": bool = false,
@"intermix-source": bool = false,
numberOfBytes: ?i32 = null,
2020-11-15 03:11:56 +01:00
signed_number: ?i64 = null,
unsigned_number: ?u64 = null,
mode: enum { default, special, slow, fast } = .default,
help: bool = false,
2020-03-04 22:37:11 +01:00
2020-03-04 22:38:26 +01:00
// This declares short-hand options for single hyphen
2020-03-04 22:37:11 +01:00
pub const shorthands = .{
.S = "intermix-source",
.b = "with-hexdump",
.O = "with-offset",
.o = "output",
};
pub const meta = .{
.option_docs = .{
2024-01-04 22:56:55 +01:00
.output = "output help",
.@"with-offset" = "with-offset help",
.@"with-hexdump" = "with-hexdump help",
.@"intermix-source" = "intermix-source",
.numberOfBytes = "numberOfBytes help",
.signed_number = "signed_number help",
.unsigned_number = "unsigned_number help",
.mode = "mode help",
.help = "help help",
2024-01-04 22:56:55 +01:00
},
};
};
const options = argsParser.parseForCurrentProcess(Options, argsAllocator, .print) catch return 1;
2020-03-04 22:37:11 +01:00
defer options.deinit();
2022-08-02 09:27:26 +02:00
std.debug.print("executable name: {?s}\n", .{options.executable_name});
2020-06-26 11:27:17 +02:00
std.debug.print("parsed options:\n", .{});
inline for (std.meta.fields(@TypeOf(options.options))) |fld| {
2021-02-15 11:01:49 +01:00
std.debug.print("\t{s} = {any}\n", .{
fld.name,
@field(options.options, fld.name),
});
}
2020-06-26 11:27:17 +02:00
std.debug.print("parsed positionals:\n", .{});
for (options.positionals) |arg| {
2021-01-04 11:37:36 +01:00
std.debug.print("\t'{s}'\n", .{arg});
2020-03-04 22:37:11 +01:00
}
2023-07-26 19:26:17 +02:00
try argsParser.printHelp(Options, options.executable_name orelse "demo", std.io.getStdOut().writer());
return 0;
2020-03-04 22:37:11 +01:00
}