-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.zig
37 lines (33 loc) · 1.11 KB
/
example.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
const std = @import("std");
const debug = std.debug;
const getopt = @import("getopt.zig");
pub fn main() void {
var arg: []const u8 = undefined;
var verbose: bool = false;
var opts = getopt.getopt("a:vh");
while (opts.next()) |maybe_opt| {
if (maybe_opt) |opt| {
switch (opt.opt) {
'a' => {
arg = opt.arg.?;
debug.print("arg = {s}\n", .{arg});
},
'v' => {
verbose = true;
debug.print("verbose = {}\n", .{verbose});
},
'h' => debug.print(
\\usage: example [-a arg] [-hv]
\\
, .{}),
else => unreachable,
}
} else break;
} else |err| {
switch (err) {
getopt.Error.InvalidOption => debug.print("invalid option: {c}\n", .{opts.optopt}),
getopt.Error.MissingArgument => debug.print("option requires an argument: {c}\n", .{opts.optopt}),
}
}
debug.print("remaining args: {?s}\n", .{opts.args()});
}