diff options
| author | 2021-04-26 16:23:15 +0200 | |
|---|---|---|
| committer | 2021-04-28 16:59:30 +0200 | |
| commit | aa334d8c1df252f48960e0253eb25544678a6023 (patch) | |
| tree | 34f23f4ac4ff10a059cff74b0e7409a4c13d2ebe /example/streaming-clap.zig | |
| parent | Add gyro.zzz (diff) | |
| download | zig-clap-aa334d8c1df252f48960e0253eb25544678a6023.tar.gz zig-clap-aa334d8c1df252f48960e0253eb25544678a6023.tar.xz zig-clap-aa334d8c1df252f48960e0253eb25544678a6023.zip | |
Refactor Diagnostic (and others) into a ParseOption struct
This allows for default arguments, which we can also extend without
breaking peoples code in the future. This is a breaking change right now
though.
Diffstat (limited to '')
| -rw-r--r-- | example/streaming-clap.zig | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig index 941070f..f8d873d 100644 --- a/example/streaming-clap.zig +++ b/example/streaming-clap.zig | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap"); | 1 | const clap = @import("clap"); |
| 2 | const std = @import("std"); | ||
| 3 | 3 | ||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | 5 | ||
| @@ -28,19 +28,18 @@ pub fn main() !void { | |||
| 28 | var iter = try clap.args.OsIterator.init(allocator); | 28 | var iter = try clap.args.OsIterator.init(allocator); |
| 29 | defer iter.deinit(); | 29 | defer iter.deinit(); |
| 30 | 30 | ||
| 31 | // Initialize our streaming parser. | 31 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 32 | // This is optional. You can also leave the `diagnostic` field unset if you | ||
| 33 | // don't care about the extra information `Diagnostic` provides. | ||
| 34 | var diag = clap.Diagnostic{}; | ||
| 32 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 35 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 33 | .params = ¶ms, | 36 | .params = ¶ms, |
| 34 | .iter = &iter, | 37 | .iter = &iter, |
| 38 | .diagnostic = &diag, | ||
| 35 | }; | 39 | }; |
| 36 | 40 | ||
| 37 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 38 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 39 | // don't care about the extra information `Diagnostics` provides. | ||
| 40 | var diag: clap.Diagnostic = undefined; | ||
| 41 | |||
| 42 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 41 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 43 | while (parser.next(&diag) catch |err| { | 42 | while (parser.next() catch |err| { |
| 44 | // Report useful error and exit | 43 | // Report useful error and exit |
| 45 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 44 | diag.report(std.io.getStdErr().outStream(), err) catch {}; |
| 46 | return err; | 45 | return err; |