diff options
| author | 2020-11-02 18:04:30 +0000 | |
|---|---|---|
| committer | 2020-11-02 19:05:41 +0100 | |
| commit | 42894f6c8b957541ac0b1830139312047cd8fdc6 (patch) | |
| tree | f5c6c839334b863f77e665879be0d467614543f2 /README.md | |
| parent | use null sentinel in OsIterator (#27) (diff) | |
| download | zig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.tar.gz zig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.tar.xz zig-clap-42894f6c8b957541ac0b1830139312047cd8fdc6.zip | |
Report error context in Diagnostic (#26)
Diffstat (limited to '')
| -rw-r--r-- | README.md | 38 |
1 files changed, 32 insertions, 6 deletions
| @@ -42,7 +42,16 @@ pub fn main() !void { | |||
| 42 | }, | 42 | }, |
| 43 | }; | 43 | }; |
| 44 | 44 | ||
| 45 | var args = try clap.parse(clap.Help, ¶ms, std.heap.page_allocator); | 45 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 46 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 47 | // don't care about the extra information `Diagnostics` provides. | ||
| 48 | var diag: clap.Diagnostic = undefined; | ||
| 49 | |||
| 50 | var args = clap.parse(clap.Help, ¶ms, std.heap.page_allocator, &diag) catch |err| { | ||
| 51 | // Report useful error and exit | ||
| 52 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | ||
| 53 | return err; | ||
| 54 | }; | ||
| 46 | defer args.deinit(); | 55 | defer args.deinit(); |
| 47 | 56 | ||
| 48 | if (args.flag("--help")) | 57 | if (args.flag("--help")) |
| @@ -66,13 +75,11 @@ const std = @import("std"); | |||
| 66 | const clap = @import("clap"); | 75 | const clap = @import("clap"); |
| 67 | 76 | ||
| 68 | pub fn main() !void { | 77 | pub fn main() !void { |
| 69 | // First we specify what parameters our program can take. | ||
| 70 | // We can use `parseParam` to parse a string to a `Param(Help)` | ||
| 71 | const params = comptime [_]clap.Param(clap.Help){ | 78 | const params = comptime [_]clap.Param(clap.Help){ |
| 72 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 79 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 73 | }; | 80 | }; |
| 74 | 81 | ||
| 75 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator); | 82 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); |
| 76 | defer args.deinit(); | 83 | defer args.deinit(); |
| 77 | 84 | ||
| 78 | _ = args.flag("--helps"); | 85 | _ = args.flag("--helps"); |
| @@ -118,14 +125,24 @@ pub fn main() !void { | |||
| 118 | .takes_value = .One, | 125 | .takes_value = .One, |
| 119 | }, | 126 | }, |
| 120 | }; | 127 | }; |
| 128 | const Clap = clap.ComptimeClap(clap.Help, clap.args.OsIterator, ¶ms); | ||
| 121 | 129 | ||
| 122 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 130 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| 123 | // wraps iterating over arguments the most efficient way on each os. | 131 | // wraps iterating over arguments the most efficient way on each os. |
| 124 | var iter = try clap.args.OsIterator.init(allocator); | 132 | var iter = try clap.args.OsIterator.init(allocator); |
| 125 | defer iter.deinit(); | 133 | defer iter.deinit(); |
| 126 | 134 | ||
| 135 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 136 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 137 | // don't care about the extra information `Diagnostics` provides. | ||
| 138 | var diag: clap.Diagnostic = undefined; | ||
| 139 | |||
| 127 | // Parse the arguments | 140 | // Parse the arguments |
| 128 | var args = try clap.ComptimeClap(clap.Help, ¶ms).parse(allocator, clap.args.OsIterator, &iter); | 141 | var args = Clap.parse(allocator, &iter, &diag) catch |err| { |
| 142 | // Report useful error and exit | ||
| 143 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | ||
| 144 | return err; | ||
| 145 | }; | ||
| 129 | defer args.deinit(); | 146 | defer args.deinit(); |
| 130 | 147 | ||
| 131 | if (args.flag("--help")) | 148 | if (args.flag("--help")) |
| @@ -182,8 +199,17 @@ pub fn main() !void { | |||
| 182 | .iter = &iter, | 199 | .iter = &iter, |
| 183 | }; | 200 | }; |
| 184 | 201 | ||
| 202 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 203 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 204 | // don't care about the extra information `Diagnostics` provides. | ||
| 205 | var diag: clap.Diagnostic = undefined; | ||
| 206 | |||
| 185 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 207 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 186 | while (try parser.next()) |arg| { | 208 | while (parser.next(&diag) catch |err| { |
| 209 | // Report useful error and exit | ||
| 210 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | ||
| 211 | return err; | ||
| 212 | }) |arg| { | ||
| 187 | // arg.param will point to the parameter which matched the argument. | 213 | // arg.param will point to the parameter which matched the argument. |
| 188 | switch (arg.param.id) { | 214 | switch (arg.param.id) { |
| 189 | 'h' => debug.warn("Help!\n", .{}), | 215 | 'h' => debug.warn("Help!\n", .{}), |