summaryrefslogtreecommitdiff
path: root/example/simple.zig
diff options
context:
space:
mode:
Diffstat (limited to 'example/simple.zig')
-rw-r--r--example/simple.zig24
1 files changed, 13 insertions, 11 deletions
diff --git a/example/simple.zig b/example/simple.zig
index ff6d301..11e975e 100644
--- a/example/simple.zig
+++ b/example/simple.zig
@@ -8,29 +8,31 @@ pub fn main() !void {
8 // First we specify what parameters our program can take. 8 // First we specify what parameters our program can take.
9 // We can use `parseParam` to parse a string to a `Param(Help)` 9 // We can use `parseParam` to parse a string to a `Param(Help)`
10 const params = comptime [_]clap.Param(clap.Help){ 10 const params = comptime [_]clap.Param(clap.Help){
11 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 11 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
12 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 12 clap.parseParam("-n, --number <usize> An option parameter, which takes a value.") catch unreachable,
13 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 13 clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable,
14 clap.parseParam("<POS>...") catch unreachable, 14 clap.parseParam("<str>...") catch unreachable,
15 }; 15 };
16 16
17 // Initalize our diagnostics, which can be used for reporting useful errors. 17 // Initalize our diagnostics, which can be used for reporting useful errors.
18 // This is optional. You can also pass `.{}` to `clap.parse` if you don't 18 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
19 // care about the extra information `Diagnostics` provides. 19 // care about the extra information `Diagnostics` provides.
20 var diag = clap.Diagnostic{}; 20 var diag = clap.Diagnostic{};
21 var args = clap.parse(clap.Help, &params, .{ .diagnostic = &diag }) catch |err| { 21 var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
22 .diagnostic = &diag,
23 }) catch |err| {
22 // Report useful error and exit 24 // Report useful error and exit
23 diag.report(io.getStdErr().writer(), err) catch {}; 25 diag.report(io.getStdErr().writer(), err) catch {};
24 return err; 26 return err;
25 }; 27 };
26 defer args.deinit(); 28 defer res.deinit();
27 29
28 if (args.flag("--help")) 30 if (res.args.help)
29 debug.print("--help\n", .{}); 31 debug.print("--help\n", .{});
30 if (args.option("--number")) |n| 32 if (res.args.number) |n|
31 debug.print("--number = {s}\n", .{n}); 33 debug.print("--number = {}\n", .{n});
32 for (args.options("--string")) |s| 34 for (res.args.string) |s|
33 debug.print("--string = {s}\n", .{s}); 35 debug.print("--string = {s}\n", .{s});
34 for (args.positionals()) |pos| 36 for (res.positionals) |pos|
35 debug.print("{s}\n", .{pos}); 37 debug.print("{s}\n", .{pos});
36} 38}