-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
demo_verb.zig
86 lines (77 loc) · 2.72 KB
/
demo_verb.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const std = @import("std");
const argsParser = @import("args");
pub fn main() !u8 {
const argsAllocator = std.heap.page_allocator;
const options = argsParser.parseWithVerbForCurrentProcess(
struct {
// this declares long option that can come before or after verb
output: ?[]const u8 = null,
// This declares short-hand options for single hyphen
pub const shorthands = .{
.o = "output",
};
},
union(enum) {
compact: struct {
// This declares long options for double hyphen
host: ?[]const u8 = null,
port: u16 = 3420,
mode: enum { default, special, slow, fast } = .default,
// This declares short-hand options for single hyphen
pub const shorthands = .{
.H = "host",
.p = "port",
};
},
reload: struct {
// This declares long options for double hyphen
force: bool = false,
// This declares short-hand options for single hyphen
pub const shorthands = .{
.f = "force",
};
},
forward: void,
@"zero-sized": struct {},
},
argsAllocator,
.print,
) catch return 1;
defer options.deinit();
std.debug.print("executable name: {?s}\n", .{options.executable_name});
// non-verb/global options
inline for (std.meta.fields(@TypeOf(options.options))) |fld| {
std.debug.print("\t{s} = {any}\n", .{
fld.name,
@field(options.options, fld.name),
});
}
// verb options
if (options.verb) |verb| {
switch (verb) {
.compact => |opts| {
inline for (std.meta.fields(@TypeOf(opts))) |fld| {
std.debug.print("\t{s} = {any}\n", .{
fld.name,
@field(opts, fld.name),
});
}
},
.reload => |opts| {
inline for (std.meta.fields(@TypeOf(opts))) |fld| {
std.debug.print("\t{s} = {any}\n", .{
fld.name,
@field(opts, fld.name),
});
}
},
.forward => std.debug.print("\t`forward` verb with no options received\n", .{}),
.@"zero-sized" => std.debug.print("\t`zero-sized` verb received\n", .{}),
}
}
std.debug.print("parsed positionals:\n", .{});
for (options.positionals) |arg| {
std.debug.print("\t'{s}'\n", .{arg});
}
return 0;
}