summaryrefslogtreecommitdiff
path: root/example/comptime-clap.zig
diff options
context:
space:
mode:
Diffstat (limited to 'example/comptime-clap.zig')
-rw-r--r--example/comptime-clap.zig45
1 files changed, 0 insertions, 45 deletions
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig
deleted file mode 100644
index e5d02ff..0000000
--- a/example/comptime-clap.zig
+++ /dev/null
@@ -1,45 +0,0 @@
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 const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, &params);
18
19 // We then initialize an argument iterator. We will use the OsIterator as it nicely
20 // wraps iterating over arguments the most efficient way on each os.
21 var iter = try clap.args.OsIterator.init(allocator);
22 defer iter.deinit();
23
24 // Initalize our diagnostics, which can be used for reporting useful errors.
25 // This is optional. You can also just pass `null` to `parser.next` if you
26 // don't care about the extra information `Diagnostics` provides.
27 var diag: clap.Diagnostic = undefined;
28
29 // Parse the arguments
30 var args = Clap.parse(allocator, &iter, &diag) catch |err| {
31 // Report useful error and exit
32 diag.report(std.io.getStdErr().outStream(), err) catch {};
33 return err;
34 };
35 defer args.deinit();
36
37 if (args.flag("--help"))
38 debug.warn("--help\n", .{});
39 if (args.option("--number")) |n|
40 debug.warn("--number = {}\n", .{n});
41 for (args.options("--string")) |s|
42 debug.warn("--string = {}\n", .{s});
43 for (args.positionals()) |pos|
44 debug.warn("{}\n", .{pos});
45}