diff options
| author | 2018-11-14 14:06:20 +0100 | |
|---|---|---|
| committer | 2018-11-14 14:06:20 +0100 | |
| commit | c564b168785e740c37f47da4942825b25cb8b4ec (patch) | |
| tree | 880252c7a63bdc91f23ba5e13b593d4ca9ad8277 /example/comptime-clap.zig | |
| parent | Zig fmt (diff) | |
| download | zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.gz zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.tar.xz zig-clap-c564b168785e740c37f47da4942825b25cb8b4ec.zip | |
Restructured and make StreamingClap simpler
* Also added a ComptimeClap
Diffstat (limited to '')
| -rw-r--r-- | example/comptime-clap.zig | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig new file mode 100644 index 0000000..e44d9b1 --- /dev/null +++ b/example/comptime-clap.zig | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | |||
| 6 | pub fn main() !void { | ||
| 7 | var direct_allocator = std.heap.DirectAllocator.init(); | ||
| 8 | const allocator = &direct_allocator.allocator; | ||
| 9 | defer direct_allocator.deinit(); | ||
| 10 | |||
| 11 | // First we specify what parameters our program can take. | ||
| 12 | const params = comptime []clap.Param(void){ | ||
| 13 | // Param.init takes 3 arguments. | ||
| 14 | // * An "id", which can be any type specified by the argument to Param. The | ||
| 15 | // ComptimeClap expects clap.Param(void) only. | ||
| 16 | // * A bool which determins wether the parameter takes a value. | ||
| 17 | // * A "Names" struct, which determins what names the parameter will have on the | ||
| 18 | // commandline. Names.prefix inits a "Names" struct that has the "short" name | ||
| 19 | // set to the first letter, and the "long" name set to the full name. | ||
| 20 | clap.Param(void).init({}, false, clap.Names.prefix("help")), | ||
| 21 | clap.Param(void).init({}, true, clap.Names.prefix("number")), | ||
| 22 | |||
| 23 | // Names.positional returns a "Names" struct where neither the "short" or "long" | ||
| 24 | // name is set. | ||
| 25 | clap.Param(void).init({}, true, clap.Names.positional()), | ||
| 26 | }; | ||
| 27 | |||
| 28 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | ||
| 29 | // wraps iterating over arguments the most efficient way on each os. | ||
| 30 | var os_iter = clap.args.OsIterator.init(allocator); | ||
| 31 | const iter = &os_iter.iter; | ||
| 32 | defer os_iter.deinit(); | ||
| 33 | |||
| 34 | // Consume the exe arg. | ||
| 35 | const exe = try iter.next(); | ||
| 36 | |||
| 37 | // Finally we can parse the arguments | ||
| 38 | var args = try clap.ComptimeClap(void, params).parse(allocator, clap.args.OsIterator.Error, iter); | ||
| 39 | defer args.deinit(); | ||
| 40 | |||
| 41 | if (args.flag("--help")) | ||
| 42 | debug.warn("Help!\n"); | ||
| 43 | if (args.option("--number")) |n| | ||
| 44 | debug.warn("--number = {}\n", n); | ||
| 45 | for (args.positionals()) |pos| | ||
| 46 | debug.warn("{}\n", pos); | ||
| 47 | } | ||