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.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 'example/simple.zig')
| -rw-r--r-- | example/simple.zig | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/example/simple.zig b/example/simple.zig index ff6d301..11e975e 100644 --- a/example/simple.zig +++ b/example/simple.zig | |||
| @@ -8,29 +8,31 @@ pub fn main() !void { | |||
| 8 | // First we specify what parameters our program can take. | 8 | // First we specify what parameters our program can take. |
| 9 | // We can use `parseParam` to parse a string to a `Param(Help)` | 9 | // We can use `parseParam` to parse a string to a `Param(Help)` |
| 10 | const params = comptime [_]clap.Param(clap.Help){ | 10 | const params = comptime [_]clap.Param(clap.Help){ |
| 11 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 11 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 12 | clap.parseParam("-n, --number <NUM> An option parameter, which takes a value.") catch unreachable, | 12 | clap.parseParam("-n, --number <usize> An option parameter, which takes a value.") catch unreachable, |
| 13 | clap.parseParam("-s, --string <STR>... An option parameter which can be specified multiple times.") catch unreachable, | 13 | clap.parseParam("-s, --string <str>... An option parameter which can be specified multiple times.") catch unreachable, |
| 14 | clap.parseParam("<POS>...") catch unreachable, | 14 | clap.parseParam("<str>...") catch unreachable, |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | // Initalize our diagnostics, which can be used for reporting useful errors. | 17 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 18 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't | 18 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 19 | // care about the extra information `Diagnostics` provides. | 19 | // care about the extra information `Diagnostics` provides. |
| 20 | var diag = clap.Diagnostic{}; | 20 | var diag = clap.Diagnostic{}; |
| 21 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | 21 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ |
| 22 | .diagnostic = &diag, | ||
| 23 | }) catch |err| { | ||
| 22 | // Report useful error and exit | 24 | // Report useful error and exit |
| 23 | diag.report(io.getStdErr().writer(), err) catch {}; | 25 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 24 | return err; | 26 | return err; |
| 25 | }; | 27 | }; |
| 26 | defer args.deinit(); | 28 | defer res.deinit(); |
| 27 | 29 | ||
| 28 | if (args.flag("--help")) | 30 | if (res.args.help) |
| 29 | debug.print("--help\n", .{}); | 31 | debug.print("--help\n", .{}); |
| 30 | if (args.option("--number")) |n| | 32 | if (res.args.number) |n| |
| 31 | debug.print("--number = {s}\n", .{n}); | 33 | debug.print("--number = {}\n", .{n}); |
| 32 | for (args.options("--string")) |s| | 34 | for (res.args.string) |s| |
| 33 | debug.print("--string = {s}\n", .{s}); | 35 | debug.print("--string = {s}\n", .{s}); |
| 34 | for (args.positionals()) |pos| | 36 | for (res.positionals) |pos| |
| 35 | debug.print("{s}\n", .{pos}); | 37 | debug.print("{s}\n", .{pos}); |
| 36 | } | 38 | } |