diff options
| author | 2021-04-26 16:23:15 +0200 | |
|---|---|---|
| committer | 2021-04-28 16:59:30 +0200 | |
| commit | aa334d8c1df252f48960e0253eb25544678a6023 (patch) | |
| tree | 34f23f4ac4ff10a059cff74b0e7409a4c13d2ebe /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 'clap.zig')
| -rw-r--r-- | clap.zig | 36 |
1 files changed, 26 insertions, 10 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: |