-
Notifications
You must be signed in to change notification settings - Fork 22
/
simple.zig
151 lines (140 loc) · 4.93 KB
/
simple.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const std = @import("std");
const cli = @import("zig-cli");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var config = struct {
ip: []const u8 = undefined,
int: i32 = undefined,
bool: bool = false,
float: f64 = 0.34,
arg1: u64 = 0,
arg2: []const []const u8 = undefined,
}{};
fn sub3(r: *cli.AppRunner) !cli.Command {
return cli.Command{
.name = "sub3",
.description = cli.Description{
.one_line = "sub3 with positional arguments",
},
.target = cli.CommandTarget{
.action = cli.CommandAction{
.positional_args = cli.PositionalArgs{
.required = try r.mkSlice(cli.PositionalArg, &.{
.{
.name = "ARG1",
.help = "arg1 help",
.value_ref = r.mkRef(&config.arg1),
},
}),
.optional = try r.mkSlice(cli.PositionalArg, &.{
.{
.name = "ARG2",
.help = "multiple arg2 help",
.value_ref = r.mkRef(&config.arg2),
},
}),
},
.exec = run_sub3,
},
},
};
}
fn parseArgs() cli.AppRunner.Error!cli.ExecFn {
// This allocator will be used to allocate config.ip and config.arg2.
var r = try cli.AppRunner.init(allocator);
const sub2 = cli.Command{
.name = "sub2",
.target = cli.CommandTarget{
.action = cli.CommandAction{
.exec = run_sub2,
},
},
};
const app = cli.App{
.command = cli.Command{
.name = "simple",
.description = cli.Description{
.one_line = "This a simple CLI app. Enjoy!",
},
.target = cli.CommandTarget{
.subcommands = &.{
cli.Command{
.name = "sub1",
.description = cli.Description{
.one_line = "another awesome command",
.detailed =
\\this is my awesome multiline description.
\\This is already line 2.
\\And this is line 3.
,
},
.options = &.{
.{
.long_name = "ip",
.help = "this is the IP address",
.short_alias = 'i',
.value_ref = r.mkRef(&config.ip),
.required = true,
.value_name = "IP",
},
.{
.long_name = "int",
.help = "this is an int\nwith the second line",
.value_ref = r.mkRef(&config.int),
},
.{
.long_name = "bool",
.short_alias = 'b',
.help = "this is a bool",
.value_ref = r.mkRef(&config.bool),
},
.{
.long_name = "float",
.help = "this is a float",
.value_ref = r.mkRef(&config.float),
},
},
.target = cli.CommandTarget{
.subcommands = &.{ sub2, try sub3(&r) },
},
},
},
},
},
.version = "0.10.3",
.author = "sam701 & contributors",
};
return r.getAction(&app);
}
pub fn main() anyerror!void {
const action = try parseArgs();
const r = action();
freeConfig();
return r;
}
// Usually, you just use an arena allocator to free all allocated resources in a batch.
// This only illustrates the fact that the config data are allocated with the allocator
// you pass to cli.AppRunner.init(allocator).
fn freeConfig() void {
allocator.free(config.ip);
if (config.arg2.len > 0) {
for (config.arg2) |item| {
allocator.free(item);
}
allocator.free(config.arg2);
}
if (gpa.deinit() == .leak) {
@panic("config leaked");
}
}
fn run_sub3() !void {
const c = &config;
std.log.debug("sub3: arg1: {}", .{c.arg1});
for (c.arg2) |arg| {
std.log.debug("sub3: arg2: {s}", .{arg});
}
}
fn run_sub2() !void {
const c = &config;
std.log.debug("running sub2: ip={s}, bool={any}, float={any}", .{ c.ip, c.bool, c.float });
}