diff options
| author | 2021-04-26 16:23:15 +0200 | |
|---|---|---|
| committer | 2021-04-28 16:59:30 +0200 | |
| commit | aa334d8c1df252f48960e0253eb25544678a6023 (patch) | |
| tree | 34f23f4ac4ff10a059cff74b0e7409a4c13d2ebe /clap | |
| 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-- | clap.zig | 36 | ||||
| -rw-r--r-- | clap/comptime.zig | 19 | ||||
| -rw-r--r-- | clap/streaming.zig | 40 |
3 files changed, 55 insertions, 40 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | 2 | ||
| 3 | const debug = std.debug; | 3 | const debug = std.debug; |
| 4 | const heap = std.heap; | ||
| 4 | const io = std.io; | 5 | const io = std.io; |
| 5 | const mem = std.mem; | 6 | const mem = std.mem; |
| 6 | const testing = std.testing; | 7 | const testing = std.testing; |
| @@ -307,7 +308,6 @@ pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | |||
| 307 | exe_arg: ?[]const u8, | 308 | exe_arg: ?[]const u8, |
| 308 | 309 | ||
| 309 | pub fn deinit(a: *@This()) void { | 310 | pub fn deinit(a: *@This()) void { |
| 310 | a.clap.deinit(); | ||
| 311 | a.arena.deinit(); | 311 | a.arena.deinit(); |
| 312 | } | 312 | } |
| 313 | 313 | ||
| @@ -329,20 +329,37 @@ pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | |||
| 329 | }; | 329 | }; |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | /// Options that can be set to customize the behavior of parsing. | ||
| 333 | pub const ParseOptions = struct { | ||
| 334 | /// The allocator used for all memory allocations. Defaults to the `heap.page_allocator`. | ||
| 335 | /// Note: You should probably override this allocator if you are calling `parseEx`. Unlike | ||
| 336 | /// `parse`, `parseEx` does not wrap the allocator so the heap allocator can be | ||
| 337 | /// quite expensive. (TODO: Can we pick a better default? For `parse`, this allocator | ||
| 338 | /// is fine, as it wraps it in an arena) | ||
| 339 | allocator: *mem.Allocator = heap.page_allocator, | ||
| 340 | diagnostic: ?*Diagnostic = null, | ||
| 341 | }; | ||
| 342 | |||
| 332 | /// Same as `parseEx` but uses the `args.OsIterator` by default. | 343 | /// Same as `parseEx` but uses the `args.OsIterator` by default. |
| 333 | pub fn parse( | 344 | pub fn parse( |
| 334 | comptime Id: type, | 345 | comptime Id: type, |
| 335 | comptime params: []const Param(Id), | 346 | comptime params: []const Param(Id), |
| 336 | allocator: *mem.Allocator, | 347 | opt: ParseOptions, |
| 337 | diag: ?*Diagnostic, | ||
| 338 | ) !Args(Id, params) { | 348 | ) !Args(Id, params) { |
| 339 | var iter = try args.OsIterator.init(allocator); | 349 | var iter = try args.OsIterator.init(opt.allocator); |
| 340 | const clap = try parseEx(Id, params, allocator, &iter, diag); | 350 | var res = Args(Id, params){ |
| 341 | return Args(Id, params){ | ||
| 342 | .arena = iter.arena, | 351 | .arena = iter.arena, |
| 343 | .clap = clap, | ||
| 344 | .exe_arg = iter.exe_arg, | 352 | .exe_arg = iter.exe_arg, |
| 353 | .clap = undefined, | ||
| 345 | }; | 354 | }; |
| 355 | |||
| 356 | // Let's reuse the arena from the `OSIterator` since we already have | ||
| 357 | // it. | ||
| 358 | res.clap = try parseEx(Id, params, &iter, .{ | ||
| 359 | .allocator = &res.arena.allocator, | ||
| 360 | .diagnostic = opt.diagnostic, | ||
| 361 | }); | ||
| 362 | return res; | ||
| 346 | } | 363 | } |
| 347 | 364 | ||
| 348 | /// Parses the command line arguments passed into the program based on an | 365 | /// Parses the command line arguments passed into the program based on an |
| @@ -350,12 +367,11 @@ pub fn parse( | |||
| 350 | pub fn parseEx( | 367 | pub fn parseEx( |
| 351 | comptime Id: type, | 368 | comptime Id: type, |
| 352 | comptime params: []const Param(Id), | 369 | comptime params: []const Param(Id), |
| 353 | allocator: *mem.Allocator, | ||
| 354 | iter: anytype, | 370 | iter: anytype, |
| 355 | diag: ?*Diagnostic, | 371 | opt: ParseOptions, |
| 356 | ) !ComptimeClap(Id, params) { | 372 | ) !ComptimeClap(Id, params) { |
| 357 | const Clap = ComptimeClap(Id, params); | 373 | const Clap = ComptimeClap(Id, params); |
| 358 | return try Clap.parse(allocator, iter, diag); | 374 | return try Clap.parse(iter, opt); |
| 359 | } | 375 | } |
| 360 | 376 | ||
| 361 | /// Will print a help message in the following format: | 377 | /// Will print a help message in the following format: |
diff --git a/clap/comptime.zig b/clap/comptime.zig index 8ab61cb..9bec38e 100644 --- a/clap/comptime.zig +++ b/clap/comptime.zig | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | const clap = @import("../clap.zig"); | 1 | const clap = @import("../clap.zig"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | const testing = std.testing; | 4 | const debug = std.debug; |
| 5 | const heap = std.heap; | 5 | const heap = std.heap; |
| 6 | const mem = std.mem; | 6 | const mem = std.mem; |
| 7 | const debug = std.debug; | 7 | const testing = std.testing; |
| 8 | 8 | ||
| 9 | /// Deprecated: Use `parseEx` instead | 9 | /// Deprecated: Use `parseEx` instead |
| 10 | pub fn ComptimeClap( | 10 | pub fn ComptimeClap( |
| @@ -42,7 +42,8 @@ pub fn ComptimeClap( | |||
| 42 | pos: []const []const u8, | 42 | pos: []const []const u8, |
| 43 | allocator: *mem.Allocator, | 43 | allocator: *mem.Allocator, |
| 44 | 44 | ||
| 45 | pub fn parse(allocator: *mem.Allocator, iter: anytype, diag: ?*clap.Diagnostic) !@This() { | 45 | pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() { |
| 46 | const allocator = opt.allocator; | ||
| 46 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; | 47 | var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options; |
| 47 | for (multis) |*multi| { | 48 | for (multis) |*multi| { |
| 48 | multi.* = std.ArrayList([]const u8).init(allocator); | 49 | multi.* = std.ArrayList([]const u8).init(allocator); |
| @@ -62,7 +63,7 @@ pub fn ComptimeClap( | |||
| 62 | .params = converted_params, | 63 | .params = converted_params, |
| 63 | .iter = iter, | 64 | .iter = iter, |
| 64 | }; | 65 | }; |
| 65 | while (try stream.next(diag)) |arg| { | 66 | while (try stream.next()) |arg| { |
| 66 | const param = arg.param; | 67 | const param = arg.param; |
| 67 | if (param.names.long == null and param.names.short == null) { | 68 | if (param.names.long == null and param.names.short == null) { |
| 68 | try pos.append(arg.value.?); | 69 | try pos.append(arg.value.?); |
| @@ -81,19 +82,17 @@ pub fn ComptimeClap( | |||
| 81 | } | 82 | } |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | for (multis) |*multi, i| { | 85 | for (multis) |*multi, i| |
| 85 | res.multi_options[i] = multi.toOwnedSlice(); | 86 | res.multi_options[i] = multi.toOwnedSlice(); |
| 86 | } | ||
| 87 | res.pos = pos.toOwnedSlice(); | 87 | res.pos = pos.toOwnedSlice(); |
| 88 | 88 | ||
| 89 | return res; | 89 | return res; |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | pub fn deinit(parser: *@This()) void { | 92 | pub fn deinit(parser: @This()) void { |
| 93 | for (parser.multi_options) |o| | 93 | for (parser.multi_options) |o| |
| 94 | parser.allocator.free(o); | 94 | parser.allocator.free(o); |
| 95 | parser.allocator.free(parser.pos); | 95 | parser.allocator.free(parser.pos); |
| 96 | parser.* = undefined; | ||
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | pub fn flag(parser: @This(), comptime name: []const u8) bool { | 98 | pub fn flag(parser: @This(), comptime name: []const u8) bool { |
| @@ -155,14 +154,12 @@ test "" { | |||
| 155 | clap.parseParam("<P>") catch unreachable, | 154 | clap.parseParam("<P>") catch unreachable, |
| 156 | }); | 155 | }); |
| 157 | 156 | ||
| 158 | var buf: [1024]u8 = undefined; | ||
| 159 | var fb_allocator = heap.FixedBufferAllocator.init(buf[0..]); | ||
| 160 | var iter = clap.args.SliceIterator{ | 157 | var iter = clap.args.SliceIterator{ |
| 161 | .args = &[_][]const u8{ | 158 | .args = &[_][]const u8{ |
| 162 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", | 159 | "-a", "-c", "0", "something", "-d", "a", "--dd", "b", |
| 163 | }, | 160 | }, |
| 164 | }; | 161 | }; |
| 165 | var args = try Clap.parse(&fb_allocator.allocator, &iter, null); | 162 | var args = try Clap.parse(&iter, .{ .allocator = testing.allocator }); |
| 166 | defer args.deinit(); | 163 | defer args.deinit(); |
| 167 | 164 | ||
| 168 | testing.expect(args.flag("-a")); | 165 | testing.expect(args.flag("-a")); |
diff --git a/clap/streaming.zig b/clap/streaming.zig index 11145f0..8030a67 100644 --- a/clap/streaming.zig +++ b/clap/streaming.zig | |||
| @@ -40,12 +40,13 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 40 | iter: *ArgIterator, | 40 | iter: *ArgIterator, |
| 41 | state: State = .normal, | 41 | state: State = .normal, |
| 42 | positional: ?*const clap.Param(Id) = null, | 42 | positional: ?*const clap.Param(Id) = null, |
| 43 | diagnostic: ?*clap.Diagnostic = null, | ||
| 43 | 44 | ||
| 44 | /// Get the next Arg that matches a Param. | 45 | /// Get the next Arg that matches a Param. |
| 45 | pub fn next(parser: *@This(), diag: ?*clap.Diagnostic) !?Arg(Id) { | 46 | pub fn next(parser: *@This()) !?Arg(Id) { |
| 46 | switch (parser.state) { | 47 | switch (parser.state) { |
| 47 | .normal => return try parser.normal(diag), | 48 | .normal => return try parser.normal(), |
| 48 | .chaining => |state| return try parser.chainging(state, diag), | 49 | .chaining => |state| return try parser.chainging(state), |
| 49 | .rest_are_positional => { | 50 | .rest_are_positional => { |
| 50 | const param = parser.positionalParam() orelse unreachable; | 51 | const param = parser.positionalParam() orelse unreachable; |
| 51 | const value = (try parser.iter.next()) orelse return null; | 52 | const value = (try parser.iter.next()) orelse return null; |
| @@ -54,7 +55,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 54 | } | 55 | } |
| 55 | } | 56 | } |
| 56 | 57 | ||
| 57 | fn normal(parser: *@This(), diag: ?*clap.Diagnostic) !?Arg(Id) { | 58 | fn normal(parser: *@This()) !?Arg(Id) { |
| 58 | const arg_info = (try parser.parseNextArg()) orelse return null; | 59 | const arg_info = (try parser.parseNextArg()) orelse return null; |
| 59 | const arg = arg_info.arg; | 60 | const arg = arg_info.arg; |
| 60 | switch (arg_info.kind) { | 61 | switch (arg_info.kind) { |
| @@ -70,7 +71,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 70 | continue; | 71 | continue; |
| 71 | if (param.takes_value == .None) { | 72 | if (param.takes_value == .None) { |
| 72 | if (maybe_value != null) | 73 | if (maybe_value != null) |
| 73 | return err(diag, arg, .{ .long = name }, error.DoesntTakeValue); | 74 | return parser.err(arg, .{ .long = name }, error.DoesntTakeValue); |
| 74 | 75 | ||
| 75 | return Arg(Id){ .param = param }; | 76 | return Arg(Id){ .param = param }; |
| 76 | } | 77 | } |
| @@ -80,18 +81,18 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 80 | break :blk v; | 81 | break :blk v; |
| 81 | 82 | ||
| 82 | break :blk (try parser.iter.next()) orelse | 83 | break :blk (try parser.iter.next()) orelse |
| 83 | return err(diag, arg, .{ .long = name }, error.MissingValue); | 84 | return parser.err(arg, .{ .long = name }, error.MissingValue); |
| 84 | }; | 85 | }; |
| 85 | 86 | ||
| 86 | return Arg(Id){ .param = param, .value = value }; | 87 | return Arg(Id){ .param = param, .value = value }; |
| 87 | } | 88 | } |
| 88 | 89 | ||
| 89 | return err(diag, arg, .{ .long = name }, error.InvalidArgument); | 90 | return parser.err(arg, .{ .long = name }, error.InvalidArgument); |
| 90 | }, | 91 | }, |
| 91 | .short => return try parser.chainging(.{ | 92 | .short => return try parser.chainging(.{ |
| 92 | .arg = arg, | 93 | .arg = arg, |
| 93 | .index = 0, | 94 | .index = 0, |
| 94 | }, diag), | 95 | }), |
| 95 | .positional => if (parser.positionalParam()) |param| { | 96 | .positional => if (parser.positionalParam()) |param| { |
| 96 | // If we find a positional with the value `--` then we | 97 | // If we find a positional with the value `--` then we |
| 97 | // interpret the rest of the arguments as positional | 98 | // interpret the rest of the arguments as positional |
| @@ -104,12 +105,12 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 104 | 105 | ||
| 105 | return Arg(Id){ .param = param, .value = arg }; | 106 | return Arg(Id){ .param = param, .value = arg }; |
| 106 | } else { | 107 | } else { |
| 107 | return err(diag, arg, .{}, error.InvalidArgument); | 108 | return parser.err(arg, .{}, error.InvalidArgument); |
| 108 | }, | 109 | }, |
| 109 | } | 110 | } |
| 110 | } | 111 | } |
| 111 | 112 | ||
| 112 | fn chainging(parser: *@This(), state: State.Chaining, diag: ?*clap.Diagnostic) !?Arg(Id) { | 113 | fn chainging(parser: *@This(), state: State.Chaining) !?Arg(Id) { |
| 113 | const arg = state.arg; | 114 | const arg = state.arg; |
| 114 | const index = state.index; | 115 | const index = state.index; |
| 115 | const next_index = index + 1; | 116 | const next_index = index + 1; |
| @@ -136,13 +137,13 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 136 | const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false; | 137 | const next_is_eql = if (next_index < arg.len) arg[next_index] == '=' else false; |
| 137 | if (param.takes_value == .None) { | 138 | if (param.takes_value == .None) { |
| 138 | if (next_is_eql) | 139 | if (next_is_eql) |
| 139 | return err(diag, arg, .{ .short = short }, error.DoesntTakeValue); | 140 | return parser.err(arg, .{ .short = short }, error.DoesntTakeValue); |
| 140 | return Arg(Id){ .param = param }; | 141 | return Arg(Id){ .param = param }; |
| 141 | } | 142 | } |
| 142 | 143 | ||
| 143 | if (arg.len <= next_index) { | 144 | if (arg.len <= next_index) { |
| 144 | const value = (try parser.iter.next()) orelse | 145 | const value = (try parser.iter.next()) orelse |
| 145 | return err(diag, arg, .{ .short = short }, error.MissingValue); | 146 | return parser.err(arg, .{ .short = short }, error.MissingValue); |
| 146 | 147 | ||
| 147 | return Arg(Id){ .param = param, .value = value }; | 148 | return Arg(Id){ .param = param, .value = value }; |
| 148 | } | 149 | } |
| @@ -153,7 +154,7 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 153 | return Arg(Id){ .param = param, .value = arg[next_index..] }; | 154 | return Arg(Id){ .param = param, .value = arg[next_index..] }; |
| 154 | } | 155 | } |
| 155 | 156 | ||
| 156 | return err(diag, arg, .{ .short = arg[index] }, error.InvalidArgument); | 157 | return parser.err(arg, .{ .short = arg[index] }, error.InvalidArgument); |
| 157 | } | 158 | } |
| 158 | 159 | ||
| 159 | fn positionalParam(parser: *@This()) ?*const clap.Param(Id) { | 160 | fn positionalParam(parser: *@This()) ?*const clap.Param(Id) { |
| @@ -194,8 +195,8 @@ pub fn StreamingClap(comptime Id: type, comptime ArgIterator: type) type { | |||
| 194 | return ArgInfo{ .arg = full_arg, .kind = .positional }; | 195 | return ArgInfo{ .arg = full_arg, .kind = .positional }; |
| 195 | } | 196 | } |
| 196 | 197 | ||
| 197 | fn err(diag: ?*clap.Diagnostic, arg: []const u8, names: clap.Names, _err: anytype) @TypeOf(_err) { | 198 | fn err(parser: @This(), arg: []const u8, names: clap.Names, _err: anytype) @TypeOf(_err) { |
| 198 | if (diag) |d| | 199 | if (parser.diagnostic) |d| |
| 199 | d.* = .{ .arg = arg, .name = names }; | 200 | d.* = .{ .arg = arg, .name = names }; |
| 200 | return _err; | 201 | return _err; |
| 201 | } | 202 | } |
| @@ -210,7 +211,7 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r | |||
| 210 | }; | 211 | }; |
| 211 | 212 | ||
| 212 | for (results) |res| { | 213 | for (results) |res| { |
| 213 | const arg = (c.next(null) catch unreachable) orelse unreachable; | 214 | const arg = (c.next() catch unreachable) orelse unreachable; |
| 214 | testing.expectEqual(res.param, arg.param); | 215 | testing.expectEqual(res.param, arg.param); |
| 215 | const expected_value = res.value orelse { | 216 | const expected_value = res.value orelse { |
| 216 | testing.expectEqual(@as(@TypeOf(arg.value), null), arg.value); | 217 | testing.expectEqual(@as(@TypeOf(arg.value), null), arg.value); |
| @@ -220,18 +221,19 @@ fn testNoErr(params: []const clap.Param(u8), args_strings: []const []const u8, r | |||
| 220 | testing.expectEqualSlices(u8, expected_value, actual_value); | 221 | testing.expectEqualSlices(u8, expected_value, actual_value); |
| 221 | } | 222 | } |
| 222 | 223 | ||
| 223 | if (c.next(null) catch unreachable) |_| | 224 | if (c.next() catch unreachable) |_| |
| 224 | unreachable; | 225 | unreachable; |
| 225 | } | 226 | } |
| 226 | 227 | ||
| 227 | fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) void { | 228 | fn testErr(params: []const clap.Param(u8), args_strings: []const []const u8, expected: []const u8) void { |
| 228 | var diag: clap.Diagnostic = undefined; | 229 | var diag = clap.Diagnostic{}; |
| 229 | var iter = args.SliceIterator{ .args = args_strings }; | 230 | var iter = args.SliceIterator{ .args = args_strings }; |
| 230 | var c = StreamingClap(u8, args.SliceIterator){ | 231 | var c = StreamingClap(u8, args.SliceIterator){ |
| 231 | .params = params, | 232 | .params = params, |
| 232 | .iter = &iter, | 233 | .iter = &iter, |
| 234 | .diagnostic = &diag, | ||
| 233 | }; | 235 | }; |
| 234 | while (c.next(&diag) catch |err| { | 236 | while (c.next() catch |err| { |
| 235 | var buf: [1024]u8 = undefined; | 237 | var buf: [1024]u8 = undefined; |
| 236 | var slice_stream = io.fixedBufferStream(&buf); | 238 | var slice_stream = io.fixedBufferStream(&buf); |
| 237 | diag.report(slice_stream.outStream(), err) catch unreachable; | 239 | diag.report(slice_stream.outStream(), err) catch unreachable; |