summaryrefslogtreecommitdiff
path: root/clap/comptime.zig
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2022-02-25 19:40:00 +0100
committerGravatar Komari Spaghetti2022-03-09 18:12:40 +0100
commit5f7b75d5523d9581eca5a72a362868ff517992e8 (patch)
tree5e874f9c935e0d7c838ea5aadf270ce2929f8e8a /clap/comptime.zig
parentBump actions/checkout from 2.4.0 to 3 (diff)
downloadzig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.gz
zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.tar.xz
zig-clap-5f7b75d5523d9581eca5a72a362868ff517992e8.zip
Allow for clap to parse argument values into types
This changes - `.flag`, `.option`, `.options` and `.positionals` are now just fields you access on the result of `parse` and `parseEx`. - `clap.ComptimeClap` has been removed. - `clap.StreamingClap` is now called `clap.streaming.Clap` - `parse` and `parseEx` now takes a `value_parsers` argument that provides the parsers to parse values. - Remove `helpEx`, `helpFull`, `usageEx` and `usageFull`. They now just expect `Id` to have methods for getting the description and value texts.
Diffstat (limited to 'clap/comptime.zig')
-rw-r--r--clap/comptime.zig237
1 files changed, 0 insertions, 237 deletions
diff --git a/clap/comptime.zig b/clap/comptime.zig
deleted file mode 100644
index b440004..0000000
--- a/clap/comptime.zig
+++ /dev/null
@@ -1,237 +0,0 @@
1const clap = @import("../clap.zig");
2const std = @import("std");
3
4const debug = std.debug;
5const heap = std.heap;
6const io = std.io;
7const mem = std.mem;
8const testing = std.testing;
9
10/// Deprecated: Use `parseEx` instead
11pub fn ComptimeClap(
12 comptime Id: type,
13 comptime params: []const clap.Param(Id),
14) type {
15 comptime var flags: usize = 0;
16 comptime var single_options: usize = 0;
17 comptime var multi_options: usize = 0;
18 comptime var converted_params: []const clap.Param(usize) = &.{};
19 for (params) |param| {
20 var index: usize = 0;
21 if (param.names.long != null or param.names.short != null) {
22 const ptr = switch (param.takes_value) {
23 .none => &flags,
24 .one => &single_options,
25 .many => &multi_options,
26 };
27 index = ptr.*;
28 ptr.* += 1;
29 }
30
31 converted_params = converted_params ++ [_]clap.Param(usize){.{
32 .id = index,
33 .names = param.names,
34 .takes_value = param.takes_value,
35 }};
36 }
37
38 return struct {
39 multi_options: [multi_options][]const []const u8,
40 single_options: [single_options][]const u8,
41 single_options_is_set: std.PackedIntArray(u1, single_options),
42 flags: std.PackedIntArray(u1, flags),
43 pos: []const []const u8,
44 allocator: mem.Allocator,
45
46 pub fn parse(iter: anytype, opt: clap.ParseOptions) !@This() {
47 const allocator = opt.allocator;
48 var multis = [_]std.ArrayList([]const u8){undefined} ** multi_options;
49 for (multis) |*multi|
50 multi.* = std.ArrayList([]const u8).init(allocator);
51
52 var pos = std.ArrayList([]const u8).init(allocator);
53
54 var res = @This(){
55 .multi_options = .{undefined} ** multi_options,
56 .single_options = .{undefined} ** single_options,
57 .single_options_is_set = std.PackedIntArray(u1, single_options).init(
58 .{0} ** single_options,
59 ),
60 .flags = std.PackedIntArray(u1, flags).init(.{0} ** flags),
61 .pos = undefined,
62 .allocator = allocator,
63 };
64
65 var stream = clap.StreamingClap(usize, @typeInfo(@TypeOf(iter)).Pointer.child){
66 .params = converted_params,
67 .iter = iter,
68 .diagnostic = opt.diagnostic,
69 };
70 while (try stream.next()) |arg| {
71 const param = arg.param;
72 if (param.names.long == null and param.names.short == null) {
73 try pos.append(arg.value.?);
74 } else if (param.takes_value == .one) {
75 debug.assert(res.single_options.len != 0);
76 if (res.single_options.len != 0) {
77 res.single_options[param.id] = arg.value.?;
78 res.single_options_is_set.set(param.id, 1);
79 }
80 } else if (param.takes_value == .many) {
81 debug.assert(multis.len != 0);
82 if (multis.len != 0)
83 try multis[param.id].append(arg.value.?);
84 } else {
85 debug.assert(res.flags.len != 0);
86 if (res.flags.len != 0)
87 res.flags.set(param.id, 1);
88 }
89 }
90
91 for (multis) |*multi, i|
92 res.multi_options[i] = multi.toOwnedSlice();
93 res.pos = pos.toOwnedSlice();
94
95 return res;
96 }
97
98 pub fn deinit(parser: @This()) void {
99 for (parser.multi_options) |o|
100 parser.allocator.free(o);
101 parser.allocator.free(parser.pos);
102 }
103
104 pub fn flag(parser: @This(), comptime name: []const u8) bool {
105 const param = comptime findParam(name);
106 if (param.takes_value != .none)
107 @compileError(name ++ " is an option and not a flag.");
108
109 return parser.flags.get(param.id) != 0;
110 }
111
112 pub fn option(parser: @This(), comptime name: []const u8) ?[]const u8 {
113 const param = comptime findParam(name);
114 if (param.takes_value == .none)
115 @compileError(name ++ " is a flag and not an option.");
116 if (param.takes_value == .many)
117 @compileError(name ++ " takes many options, not one.");
118 if (parser.single_options_is_set.get(param.id) == 0)
119 return null;
120 return parser.single_options[param.id];
121 }
122
123 pub fn options(parser: @This(), comptime name: []const u8) []const []const u8 {
124 const param = comptime findParam(name);
125 if (param.takes_value == .none)
126 @compileError(name ++ " is a flag and not an option.");
127 if (param.takes_value == .one)
128 @compileError(name ++ " takes one option, not multiple.");
129
130 return parser.multi_options[param.id];
131 }
132
133 pub fn positionals(parser: @This()) []const []const u8 {
134 return parser.pos;
135 }
136
137 fn findParam(comptime name: []const u8) clap.Param(usize) {
138 comptime {
139 for (converted_params) |param| {
140 if (param.names.short) |s| {
141 if (mem.eql(u8, name, "-" ++ [_]u8{s}))
142 return param;
143 }
144 if (param.names.long) |l| {
145 if (mem.eql(u8, name, "--" ++ l))
146 return param;
147 }
148 }
149
150 @compileError(name ++ " is not a parameter.");
151 }
152 }
153 };
154}
155
156test "" {
157 const params = comptime &.{
158 clap.parseParam("-a, --aa") catch unreachable,
159 clap.parseParam("-b, --bb") catch unreachable,
160 clap.parseParam("-c, --cc <V>") catch unreachable,
161 clap.parseParam("-d, --dd <V>...") catch unreachable,
162 clap.parseParam("<P>") catch unreachable,
163 };
164
165 var iter = clap.args.SliceIterator{
166 .args = &.{
167 "-a", "-c", "0", "something", "-d", "a", "--dd", "b",
168 },
169 };
170 var args = try clap.parseEx(clap.Help, params, &iter, .{ .allocator = testing.allocator });
171 defer args.deinit();
172
173 try testing.expect(args.flag("-a"));
174 try testing.expect(args.flag("--aa"));
175 try testing.expect(!args.flag("-b"));
176 try testing.expect(!args.flag("--bb"));
177 try testing.expectEqualStrings("0", args.option("-c").?);
178 try testing.expectEqualStrings("0", args.option("--cc").?);
179 try testing.expectEqual(@as(usize, 1), args.positionals().len);
180 try testing.expectEqualStrings("something", args.positionals()[0]);
181 try testing.expectEqualSlices([]const u8, &.{ "a", "b" }, args.options("-d"));
182 try testing.expectEqualSlices([]const u8, &.{ "a", "b" }, args.options("--dd"));
183}
184
185test "empty" {
186 var iter = clap.args.SliceIterator{ .args = &.{} };
187 var args = try clap.parseEx(u8, &.{}, &iter, .{ .allocator = testing.allocator });
188 defer args.deinit();
189}
190
191fn 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 _ = 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
212test "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(&params, &.{"q"}, "Invalid argument 'q'\n");
226 try testErr(&params, &.{"-q"}, "Invalid argument '-q'\n");
227 try testErr(&params, &.{"--q"}, "Invalid argument '--q'\n");
228 try testErr(&params, &.{"--q=1"}, "Invalid argument '--q'\n");
229 try testErr(&params, &.{"-a=1"}, "The argument '-a' does not take a value\n");
230 try testErr(&params, &.{"--aa=1"}, "The argument '--aa' does not take a value\n");
231 try testErr(&params, &.{"-c"}, "The argument '-c' requires a value but none was supplied\n");
232 try testErr(
233 &params,
234 &.{"--cc"},
235 "The argument '--cc' requires a value but none was supplied\n",
236 );
237}