diff options
| author | 2022-02-25 19:40:00 +0100 | |
|---|---|---|
| committer | 2022-03-09 18:12:40 +0100 | |
| commit | 5f7b75d5523d9581eca5a72a362868ff517992e8 (patch) | |
| tree | 5e874f9c935e0d7c838ea5aadf270ce2929f8e8a /example/simple-ex.zig | |
| parent | Bump actions/checkout from 2.4.0 to 3 (diff) | |
| download | zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.gz zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.xz zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.zip | |
Allow for clap to parse argument values into types
This changes
- `.flag`, `.option`, `.options` and `.positionals` are now just fields
you access on the result of `parse` and `parseEx`.
- `clap.ComptimeClap` has been removed.
- `clap.StreamingClap` is now called `clap.streaming.Clap`
- `parse` and `parseEx` now takes a `value_parsers` argument that
provides the parsers to parse values.
- Remove `helpEx`, `helpFull`, `usageEx` and `usageFull`. They now just
expect `Id` to have methods for getting the description and value
texts.
Diffstat (limited to '')
| -rw-r--r-- | example/simple-ex.zig | 39 |
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; | |||
| 6 | const process = std.process; | 6 | const process = std.process; |
| 7 | 7 | ||
| 8 | pub fn main() !void { | 8 | pub 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, ¶ms, &iter, .{ | 27 | var res = clap.parse(clap.Help, ¶ms, 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 | } |