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
|
const clap = @import("clap");
const std = @import("std");
const debug = std.debug;
const io = std.io;
pub fn main() !void {
// First we specify what parameters our program can take.
// We can use `parseParam` to parse a string to a `Param(Help)`
const params = comptime [_]clap.Param(clap.Help){
clap.untyped.parseParam("-h, --help Display this help and exit.") catch unreachable,
clap.untyped.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
clap.untyped.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
clap.untyped.parseParam("<POS>...") catch unreachable,
};
// Initalize our diagnostics, which can be used for reporting useful errors.
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
// care about the extra information `Diagnostics` provides.
var diag = clap.Diagnostic{};
var res = clap.untyped.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| {
// Report useful error and exit
diag.report(io.getStdErr().writer(), err) catch {};
return err;
};
defer res.deinit();
if (res.args.help)
debug.print("--help\n", .{});
if (res.args.number) |n|
debug.print("--number = {s}\n", .{n});
for (res.args.string) |s|
debug.print("--string = {s}\n", .{s});
for (res.positionals) |pos|
debug.print("{s}\n", .{pos});
}
|