summaryrefslogtreecommitdiff
path: root/clap.zig
diff options
context:
space:
mode:
Diffstat (limited to 'clap.zig')
-rw-r--r--clap.zig36
1 files changed, 26 insertions, 10 deletions
diff --git a/clap.zig b/clap.zig
index 4548a48..b31cd1d 100644
--- a/clap.zig
+++ b/clap.zig
@@ -1,6 +1,7 @@
1const std = @import("std"); 1const std = @import("std");
2 2
3const debug = std.debug; 3const debug = std.debug;
4const heap = std.heap;
4const io = std.io; 5const io = std.io;
5const mem = std.mem; 6const mem = std.mem;
6const testing = std.testing; 7const 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.
333pub 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.
333pub fn parse( 344pub 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(
350pub fn parseEx( 367pub 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: