diff options
| author | 2021-04-26 16:23:15 +0200 | |
|---|---|---|
| committer | 2021-04-28 16:59:30 +0200 | |
| commit | aa334d8c1df252f48960e0253eb25544678a6023 (patch) | |
| tree | 34f23f4ac4ff10a059cff74b0e7409a4c13d2ebe /example | |
| 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 'example')
| -rw-r--r-- | example/simple-ex.zig | 14 | ||||
| -rw-r--r-- | example/simple.zig | 11 | ||||
| -rw-r--r-- | example/streaming-clap.zig | 15 |
3 files changed, 20 insertions, 20 deletions
diff --git a/example/simple-ex.zig b/example/simple-ex.zig index d6ecc44..f504d63 100644 --- a/example/simple-ex.zig +++ b/example/simple-ex.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 | ||
| @@ -21,11 +21,13 @@ pub fn main() !void { | |||
| 21 | defer iter.deinit(); | 21 | defer iter.deinit(); |
| 22 | 22 | ||
| 23 | // Initalize our diagnostics, which can be used for reporting useful errors. | 23 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 24 | // This is optional. You can also just pass `null` to `parser.next` if you | 24 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 25 | // don't care about the extra information `Diagnostics` provides. | 25 | // care about the extra information `Diagnostics` provides. |
| 26 | var diag: clap.Diagnostic = undefined; | 26 | var diag = clap.Diagnostic{}; |
| 27 | 27 | var args = clap.parseEx(clap.Help, ¶ms, &iter, .{ | |
| 28 | var args = clap.parseEx(clap.Help, ¶ms, allocator, &iter, &diag) catch |err| { | 28 | .allocator = allocator, |
| 29 | .diagnostic = &diag, | ||
| 30 | }) catch |err| { | ||
| 29 | // Report useful error and exit | 31 | // Report useful error and exit |
| 30 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 32 | diag.report(std.io.getStdErr().outStream(), err) catch {}; |
| 31 | return err; | 33 | return err; |
diff --git a/example/simple.zig b/example/simple.zig index 270e344..392dca3 100644 --- a/example/simple.zig +++ b/example/simple.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 | ||
| @@ -14,11 +14,10 @@ pub fn main() !void { | |||
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | // Initalize our diagnostics, which can be used for reporting useful errors. | 16 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 17 | // This is optional. You can also just pass `null` to `parser.next` if you | 17 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 18 | // don't care about the extra information `Diagnostics` provides. | 18 | // care about the extra information `Diagnostics` provides. |
| 19 | var diag: clap.Diagnostic = undefined; | 19 | var diag = clap.Diagnostic{}; |
| 20 | 20 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | |
| 21 | var args = clap.parse(clap.Help, ¶ms, std.heap.page_allocator, &diag) catch |err| { | ||
| 22 | // Report useful error and exit | 21 | // Report useful error and exit |
| 23 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 22 | diag.report(std.io.getStdErr().outStream(), err) catch {}; |
| 24 | return err; | 23 | return err; |
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; |