summaryrefslogtreecommitdiff
path: root/clap/comptime.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--clap/comptime.zig39
1 files changed, 18 insertions, 21 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig
index 8ab61cb..122ff16 100644
--- a/clap/comptime.zig
+++ b/clap/comptime.zig
@@ -1,10 +1,10 @@
1const clap = @import("../clap.zig"); 1const clap = @import("../clap.zig");
2const std = @import("std"); 2const std = @import("std");
3 3
4const testing = std.testing; 4const debug = std.debug;
5const heap = std.heap; 5const heap = std.heap;
6const mem = std.mem; 6const mem = std.mem;
7const debug = std.debug; 7const testing = std.testing;
8 8
9/// Deprecated: Use `parseEx` instead 9/// Deprecated: Use `parseEx` instead
10pub fn ComptimeClap( 10pub fn ComptimeClap(
@@ -19,9 +19,9 @@ pub fn ComptimeClap(
19 var index: usize = 0; 19 var index: usize = 0;
20 if (param.names.long != null or param.names.short != null) { 20 if (param.names.long != null or param.names.short != null) {
21 const ptr = switch (param.takes_value) { 21 const ptr = switch (param.takes_value) {
22 .None => &flags, 22 .none => &flags,
23 .One => &single_options, 23 .one => &single_options,
24 .Many => &multi_options, 24 .many => &multi_options,
25 }; 25 };
26 index = ptr.*; 26 index = ptr.*;
27 ptr.* += 1; 27 ptr.* += 1;
@@ -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,15 +63,15 @@ 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.?);
69 } else if (param.takes_value == .One) { 70 } else if (param.takes_value == .one) {
70 debug.assert(res.single_options.len != 0); 71 debug.assert(res.single_options.len != 0);
71 if (res.single_options.len != 0) 72 if (res.single_options.len != 0)
72 res.single_options[param.id] = arg.value.?; 73 res.single_options[param.id] = arg.value.?;
73 } else if (param.takes_value == .Many) { 74 } else if (param.takes_value == .many) {
74 debug.assert(multis.len != 0); 75 debug.assert(multis.len != 0);
75 if (multis.len != 0) 76 if (multis.len != 0)
76 try multis[param.id].append(arg.value.?); 77 try multis[param.id].append(arg.value.?);
@@ -81,24 +82,22 @@ 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 {
100 const param = comptime findParam(name); 99 const param = comptime findParam(name);
101 if (param.takes_value != .None) 100 if (param.takes_value != .none)
102 @compileError(name ++ " is an option and not a flag."); 101 @compileError(name ++ " is an option and not a flag.");
103 102
104 return parser.flags[param.id]; 103 return parser.flags[param.id];
@@ -106,18 +105,18 @@ pub fn ComptimeClap(
106 105
107 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 { 106 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 {
108 const param = comptime findParam(name); 107 const param = comptime findParam(name);
109 if (param.takes_value == .None) 108 if (param.takes_value == .none)
110 @compileError(name ++ " is a flag and not an option."); 109 @compileError(name ++ " is a flag and not an option.");
111 if (param.takes_value == .Many) 110 if (param.takes_value == .many)
112 @compileError(name ++ " takes many options, not one."); 111 @compileError(name ++ " takes many options, not one.");
113 return parser.single_options[param.id]; 112 return parser.single_options[param.id];
114 } 113 }
115 114
116 pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 { 115 pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 {
117 const param = comptime findParam(name); 116 const param = comptime findParam(name);
118 if (param.takes_value == .None) 117 if (param.takes_value == .none)
119 @compileError(name ++ " is a flag and not an option."); 118 @compileError(name ++ " is a flag and not an option.");
120 if (param.takes_value == .One) 119 if (param.takes_value == .one)
121 @compileError(name ++ " takes one option, not multiple."); 120 @compileError(name ++ " takes one option, not multiple.");
122 121
123 return parser.multi_options[param.id]; 122 return parser.multi_options[param.id];
@@ -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"));