diff options
| author | 2021-07-16 15:13:57 +0000 | |
|---|---|---|
| committer | 2021-07-16 15:13:57 +0000 | |
| commit | bb8a5ae5b9eec884a50e4977cc5275039ea53207 (patch) | |
| tree | 4d038fab32bd47b8b7f6e4f5f81523561cf2efb5 | |
| parent | Update example of `usage` (#45) (diff) | |
| download | zig-clap-bb8a5ae5b9eec884a50e4977cc5275039ea53207.tar.gz zig-clap-bb8a5ae5b9eec884a50e4977cc5275039ea53207.tar.xz zig-clap-bb8a5ae5b9eec884a50e4977cc5275039ea53207.zip | |
Forward diagnostics down to StreamingClap
fixes #46
| -rw-r--r-- | clap/comptime.zig | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig index 0328997..a0f57ad 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -3,6 +3,7 @@ const std = @import("std"); | |||
| 3 | 3 | ||
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | const heap = std.heap; | 5 | const heap = std.heap; |
| 6 | const io = std.io; | ||
| 6 | const mem = std.mem; | 7 | const mem = std.mem; |
| 7 | const testing = std.testing; | 8 | const testing = std.testing; |
| 8 | 9 | ||
| @@ -64,6 +65,7 @@ pub fn ComptimeClap( | |||
| 64 | var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){ | 65 | var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){ |
| 65 | .params = converted_params, | 66 | .params = converted_params, |
| 66 | .iter = iter, | 67 | .iter = iter, |
| 68 | .diagnostic = opt.diagnostic, | ||
| 67 | }; | 69 | }; |
| 68 | while (try stream.next()) |arg| { | 70 | while (try stream.next()) |arg| { |
| 69 | const param = arg.param; | 71 | const param = arg.param; |
| @@ -152,20 +154,20 @@ pub fn ComptimeClap( | |||
| 152 | } | 154 | } |
| 153 | 155 | ||
| 154 | test "" { | 156 | test "" { |
| 155 | const Clap = ComptimeClap(clap.Help, comptime &.{ | 157 | const params = comptime &.{ |
| 156 | clap.parseParam("-a, --aa") catch unreachable, | 158 | clap.parseParam("-a, --aa") catch unreachable, |
| 157 | clap.parseParam("-b, --bb") catch unreachable, | 159 | clap.parseParam("-b, --bb") catch unreachable, |
| 158 | clap.parseParam("-c, --cc <V>") catch unreachable, | 160 | clap.parseParam("-c, --cc <V>") catch unreachable, |
| 159 | clap.parseParam("-d, --dd <V>...") catch unreachable, | 161 | clap.parseParam("-d, --dd <V>...") catch unreachable, |
| 160 | clap.parseParam("<P>") catch unreachable, | 162 | clap.parseParam("<P>") catch unreachable, |
| 161 | }); | 163 | }; |
| 162 | 164 | ||
| 163 | var iter = clap.args.SliceIterator{ | 165 | var iter = clap.args.SliceIterator{ |
| 164 | .args = &.{ | 166 | .args = &.{ |
| 165 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", | 167 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", |
| 166 | }, | 168 | }, |
| 167 | }; | 169 | }; |
| 168 | var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); | 170 | var args = try clap.parseEx(clap.Help, params, &iter, .{ .allocator = testing.allocator }); |
| 169 | defer args.deinit(); | 171 | defer args.deinit(); |
| 170 | 172 | ||
| 171 | try testing.expect(args.flag("-a")); | 173 | try testing.expect(args.flag("-a")); |
| @@ -181,8 +183,55 @@ test "" { | |||
| 181 | } | 183 | } |
| 182 | 184 | ||
| 183 | test "empty" { | 185 | test "empty" { |
| 184 | const Clap = ComptimeClap(clap.Help, comptime &.{}); | ||
| 185 | var iter = clap.args.SliceIterator{ .args = &.{} }; | 186 | var iter = clap.args.SliceIterator{ .args = &.{} }; |
| 186 | var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); | 187 | var args = try clap.parseEx(u8, &.{}, &iter, .{ .allocator = testing.allocator }); |
| 187 | defer args.deinit(); | 188 | defer args.deinit(); |
| 188 | } | 189 | } |
| 190 | |||
| 191 | fn testErr( | ||
| 192 | comptime params: []const clap.Param(u8), | ||
| 193 | args_strings: []const []const u8, | ||
| 194 | expected: []const u8, | ||
| 195 | ) !void { | ||
| 196 | var diag = clap.Diagnostic{}; | ||
| 197 | var iter = clap.args.SliceIterator{ .args = args_strings }; | ||
| 198 | var args = clap.parseEx(u8, params, &iter, .{ | ||
| 199 | .allocator = testing.allocator, | ||
| 200 | .diagnostic = &diag, | ||
| 201 | }) catch |err| { | ||
| 202 | var buf: [1024]u8 = undefined; | ||
| 203 | var fbs = io.fixedBufferStream(&buf); | ||
| 204 | diag.report(fbs.writer(), err) catch return error.TestFailed; | ||
| 205 | try testing.expectEqualStrings(expected, fbs.getWritten()); | ||
| 206 | return; | ||
| 207 | }; | ||
| 208 | |||
| 209 | try testing.expect(false); | ||
| 210 | } | ||
| 211 | |||
| 212 | test "errors" { | ||
| 213 | const params = [_]clap.Param(u8){ | ||
| 214 | .{ | ||
| 215 | .id = 0, | ||
| 216 | .names = .{ .short = 'a', .long = "aa" }, | ||
| 217 | }, | ||
| 218 | .{ | ||
| 219 | .id = 1, | ||
| 220 | .names = .{ .short = 'c', .long = "cc" }, | ||
| 221 | .takes_value = .one, | ||
| 222 | }, | ||
| 223 | }; | ||
| 224 | |||
| 225 | try testErr(¶ms, &.{"q"}, "Invalid argument 'q'\n"); | ||
| 226 | try testErr(¶ms, &.{"-q"}, "Invalid argument '-q'\n"); | ||
| 227 | try testErr(¶ms, &.{"--q"}, "Invalid argument '--q'\n"); | ||
| 228 | try testErr(¶ms, &.{"--q=1"}, "Invalid argument '--q'\n"); | ||
| 229 | try testErr(¶ms, &.{"-a=1"}, "The argument '-a' does not take a value\n"); | ||
| 230 | try testErr(¶ms, &.{"--aa=1"}, "The argument '--aa' does not take a value\n"); | ||
| 231 | try testErr(¶ms, &.{"-c"}, "The argument '-c' requires a value but none was supplied\n"); | ||
| 232 | try testErr( | ||
| 233 | ¶ms, | ||
| 234 | &.{"--cc"}, | ||
| 235 | "The argument '--cc' requires a value but none was supplied\n", | ||
| 236 | ); | ||
| 237 | } | ||