summaryrefslogtreecommitdiff
path: root/example/simple-ex.zig
diff options
context:
space:
mode:
Diffstat (limited to 'example/simple-ex.zig')
-rw-r--r--example/simple-ex.zig39
1 files changed, 17 insertions, 22 deletions
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
index d2dc77e..6cb4c3f 100644
--- a/example/simple-ex.zig
+++ b/example/simple-ex.zig
@@ -6,43 +6,38 @@ const io = std.io;
6const process = std.process; 6const process = std.process;
7 7
8pub fn main() !void { 8pub fn main() !void {
9 const allocator = std.heap.page_allocator;
10
11 // First we specify what parameters our program can take. 9 // First we specify what parameters our program can take.
12 // We can use `parseParam` to parse a string to a `Param(Help)` 10 // We can use `parseParam` to parse a string to a `Param(Help)`
13 const params = comptime [_]clap.Param(clap.Help){ 11 const params = comptime [_]clap.Param(clap.Help){
14 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, 12 clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
15 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, 13 clap.parseParam("-n, --number <INT> An option parameter, which takes a value.") catch unreachable,
16 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, 14 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
17 clap.parseParam("<POS>...") catch unreachable, 15 clap.parseParam("<FILE>...") catch unreachable,
18 }; 16 };
19 17
20 var iter = try process.ArgIterator.initWithAllocator(allocator); 18 // Declare our own parsers which are used to map the argument strings to other
21 defer iter.deinit(); 19 // types.
22 20 const parsers = comptime .{
23 // Skip exe argument 21 .STR = clap.parsers.string,
24 _ = iter.next(); 22 .FILE = clap.parsers.string,
23 .INT = clap.parsers.int(usize, 10),
24 };
25 25
26 // Initalize our diagnostics, which can be used for reporting useful errors.
27 // This is optional. You can also pass `.{}` to `clap.parse` if you don't
28 // care about the extra information `Diagnostics` provides.
29 var diag = clap.Diagnostic{}; 26 var diag = clap.Diagnostic{};
30 var args = clap.parseEx(clap.Help, &params, &iter, .{ 27 var res = clap.parse(clap.Help, &params, parsers, .{
31 .allocator = allocator,
32 .diagnostic = &diag, 28 .diagnostic = &diag,
33 }) catch |err| { 29 }) catch |err| {
34 // Report useful error and exit
35 diag.report(io.getStdErr().writer(), err) catch {}; 30 diag.report(io.getStdErr().writer(), err) catch {};
36 return err; 31 return err;
37 }; 32 };
38 defer args.deinit(); 33 defer res.deinit();
39 34
40 if (args.flag("--help")) 35 if (res.args.help)
41 debug.print("--help\n", .{}); 36 debug.print("--help\n", .{});
42 if (args.option("--number")) |n| 37 if (res.args.number) |n|
43 debug.print("--number = {s}\n", .{n}); 38 debug.print("--number = {}\n", .{n});
44 for (args.options("--string")) |s| 39 for (res.args.string) |s|
45 debug.print("--string = {s}\n", .{s}); 40 debug.print("--string = {s}\n", .{s});
46 for (args.positionals()) |pos| 41 for (res.positionals) |pos|
47 debug.print("{s}\n", .{pos}); 42 debug.print("{s}\n", .{pos});
48} 43}