summaryrefslogtreecommitdiff
path: root/example/simple-ex.zig
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2020-11-10 18:53:57 +0100
committerGravatar Jimmi Holst Christensen2020-11-10 18:53:57 +0100
commitb7e6ebf36e2ac4314e6bff65f1d64466ea82a18a (patch)
treef979a760149f4c7a2d9b9113a872d8d7e6d56bef /example/simple-ex.zig
parentUpdate ci to use 0.7.0 (diff)
downloadzig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.tar.gz
zig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.tar.xz
zig-clap-b7e6ebf36e2ac4314e6bff65f1d64466ea82a18a.zip
Deprecate ComptimeClap in favor of parseExv0.3.0
Diffstat (limited to 'example/simple-ex.zig')
-rw-r--r--example/simple-ex.zig43
1 files changed, 43 insertions, 0 deletions
diff --git a/example/simple-ex.zig b/example/simple-ex.zig
new file mode 100644
index 0000000..d6ecc44
--- /dev/null
+++ b/example/simple-ex.zig
@@ -0,0 +1,43 @@
1const std = @import("std");
2const clap = @import("clap");
3
4const debug = std.debug;
5
6pub fn main() !void {
7 const allocator = std.heap.page_allocator;
8
9 // First we specify what parameters our program can take.
10 // We can use `parseParam` to parse a string to a `Param(Help)`
11 const params = comptime [_]clap.Param(clap.Help){
12 clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
13 clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable,
14 clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable,
15 clap.parseParam("<POS>...") catch unreachable,
16 };
17
18 // We then initialize an argument iterator. We will use the OsIterator as it nicely
19 // wraps iterating over arguments the most efficient way on each os.
20 var iter = try clap.args.OsIterator.init(allocator);
21 defer iter.deinit();
22
23 // Initalize our diagnostics, which can be used for reporting useful errors.
24 // This is optional. You can also just pass `null` to `parser.next` if you
25 // don't care about the extra information `Diagnostics` provides.
26 var diag: clap.Diagnostic = undefined;
27
28 var args = clap.parseEx(clap.Help, &params, allocator, &iter, &diag) catch |err| {
29 // Report useful error and exit
30 diag.report(std.io.getStdErr().outStream(), err) catch {};
31 return err;
32 };
33 defer args.deinit();
34
35 if (args.flag("--help"))
36 debug.warn("--help\n", .{});
37 if (args.option("--number")) |n|
38 debug.warn("--number = {}\n", .{n});
39 for (args.options("--string")) |s|
40 debug.warn("--string = {}\n", .{s});
41 for (args.positionals()) |pos|
42 debug.warn("{}\n", .{pos});
43}