-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.zig
68 lines (58 loc) · 1.69 KB
/
options.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
const std = @import("std");
const app = @import("argz");
const Command = app.Command;
const Option = app.Option;
const ValueType = app.ValueType;
const print = std.debug.print;
pub fn myRootCommand(c: *Command) anyerror!void {
if (c.getOption("i")) |o| if (o.intValue()) |v| {
print("value of int option: {d}\n", .{v});
};
if (c.getOption("s")) |o| if (o.stringValue()) |v| {
print("value of string option: {s}\n", .{v});
};
if (c.getOption("b")) |o| if (o.boolValue()) |v| {
print("value of boolean option: {}\n", .{v});
};
if (c.getFlag("f")) |o| if (o.boolValue()) |v| {
print("value of flag: {}\n", .{v});
};
}
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{
.allocator = allocator,
.name = "root",
.run = myRootCommand,
};
defer root.deinit();
const intop = Option{
.type = ValueType.int,
.names = &.{ "int-option", "i" },
.default = "10",
.required = false,
};
const strop = Option{
.type = ValueType.string,
.names = &.{ "str-option", "s" },
.default = "mystring",
.required = false,
};
const boolop = Option{
.type = ValueType.boolean,
.names = &.{ "bool-option", "b" },
.default = "true",
.required = false,
};
const flag = Option{
.is_flag = true,
.names = &.{ "my-flag", "f" },
};
try root.addOptions(&.{ intop, strop, boolop, flag });
try root.parseAndStart();
}