-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.zig
58 lines (47 loc) · 1.3 KB
/
help.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
const std = @import("std");
const lib = @import("argz");
const Command = lib.Command;
const Option = lib.Option;
const printf = std.debug.print;
fn print(s: []const u8) void {
printf("{s}", .{s});
}
const Error = error{
GenericError,
};
pub fn runRoot(c: *Command) anyerror!void {
print("running root ...\n");
if (c.getFlag("f")) |f| {
if (f.value.?.boolean) {
print("bool true in root command\n");
} else {
print("bool false in root command\n");
}
} else {
print("flag not found");
return error.GenericError;
}
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const check = gpa.deinit();
if (check == .leak) @panic("memory leaked");
}
const allocator = gpa.allocator();
var root = Command.init(allocator, "root");
root.run = runRoot;
const o1 = Option{
.names = &.{ "f", "my-flag" },
.is_flag = true,
.description = "flag option description",
};
try root.addOption(o1);
var sub1 = Command.init(allocator, "sub1");
sub1.description = "my sub command description";
try root.addCommand(sub1);
defer root.deinit();
root.description = "root command desc";
try root.parse();
try root.start();
}