summaryrefslogtreecommitdiff
path: root/index.zig
diff options
context:
space:
mode:
authorGravatar Jimmi Holst Christensen2018-05-20 12:51:43 +0200
committerGravatar Jimmi Holst Christensen2018-05-20 12:51:43 +0200
commit661d32ded068ecff486a952626bd209240035781 (patch)
treef2f10528cd2b98bc8ea5b193da54577da094e3bf /index.zig
parentRemoved false or none tested advertisement from README (diff)
downloadzig-clap-661d32ded068ecff486a952626bd209240035781.tar.gz
zig-clap-661d32ded068ecff486a952626bd209240035781.tar.xz
zig-clap-661d32ded068ecff486a952626bd209240035781.zip
Support for command params in core.zig
* Also refactored structure * related: #1
Diffstat (limited to '')
-rw-r--r--index.zig (renamed from extended.zig)89
1 files changed, 66 insertions, 23 deletions
diff --git a/extended.zig b/index.zig
index a5c8e89..7b82211 100644
--- a/extended.zig
+++ b/index.zig
@@ -1,6 +1,7 @@
1pub const core = @import("core.zig");
2
1const builtin = @import("builtin"); 3const builtin = @import("builtin");
2const std = @import("std"); 4const std = @import("std");
3const core = @import("core.zig");
4 5
5const mem = std.mem; 6const mem = std.mem;
6const fmt = std.fmt; 7const fmt = std.fmt;
@@ -17,8 +18,45 @@ pub const Param = struct {
17 required: bool, 18 required: bool,
18 position: ?usize, 19 position: ?usize,
19 20
20 pub fn init(name: []const u8) Param { 21 pub fn short(s: u8) Param {
21 return Param { 22 return Param{
23 .field = []u8{s},
24 .short = s,
25 .long = null,
26 .takes_value = null,
27 .required = false,
28 .position = null,
29 };
30 }
31
32 pub fn long(l: []const u8) Param {
33 return Param{
34 .field = l,
35 .short = null,
36 .long = l,
37 .takes_value = null,
38 .required = false,
39 .position = null,
40 };
41 }
42
43 pub fn value(f: []const u8) Param {
44 return Param{
45 .field = f,
46 .short = null,
47 .long = null,
48 .takes_value = null,
49 .required = false,
50 .position = null,
51 };
52 }
53
54 /// Initialize a ::Param.
55 /// If ::name.len == 0, then it's a value parameter: "value".
56 /// If ::name.len == 1, then it's a short parameter: "-s".
57 /// If ::name.len > 1, then it's a long parameter: "--long".
58 pub fn smart(name: []const u8) Param {
59 return Param{
22 .field = name, 60 .field = name,
23 .short = if (name.len == 1) name[0] else null, 61 .short = if (name.len == 1) name[0] else null,
24 .long = if (name.len > 1) name else null, 62 .long = if (name.len > 1) name else null,
@@ -28,9 +66,9 @@ pub const Param = struct {
28 }; 66 };
29 } 67 }
30 68
31 pub fn with(param: &const Param, comptime field_name: []const u8, value: var) Param { 69 pub fn with(param: &const Param, comptime field_name: []const u8, v: var) Param {
32 var res = *param; 70 var res = *param;
33 @field(res, field_name) = value; 71 @field(res, field_name) = v;
34 return res; 72 return res;
35 } 73 }
36}; 74};
@@ -50,6 +88,7 @@ pub fn Clap(comptime Result: type) type {
50 for (clap.params) |p, i| { 88 for (clap.params) |p, i| {
51 res[i] = core.Param(usize) { 89 res[i] = core.Param(usize) {
52 .id = i, 90 .id = i,
91 .command = null,
53 .short = p.short, 92 .short = p.short,
54 .long = p.long, 93 .long = p.long,
55 .takes_value = p.takes_value != null, 94 .takes_value = p.takes_value != null,
@@ -189,13 +228,17 @@ fn testErr(comptime clap: &const Clap(Options), args: []const []const u8, expect
189 } 228 }
190} 229}
191 230
192test "clap.parse: short" { 231test "clap.core" {
232 _ = core;
233}
234
235test "clap: short" {
193 const clap = comptime Clap(Options) { 236 const clap = comptime Clap(Options) {
194 .defaults = default, 237 .defaults = default,
195 .params = []Param { 238 .params = []Param {
196 Param.init("a"), 239 Param.smart("a"),
197 Param.init("b"), 240 Param.smart("b"),
198 Param.init("int") 241 Param.smart("int")
199 .with("short", 'i') 242 .with("short", 'i')
200 .with("takes_value", Parser.int(i64, 10)) 243 .with("takes_value", Parser.int(i64, 10))
201 } 244 }
@@ -212,14 +255,14 @@ test "clap.parse: short" {
212 testNoErr(clap, [][]const u8 { "-abi100" }, default.with("a", true).with("b", true).with("int", 100)); 255 testNoErr(clap, [][]const u8 { "-abi100" }, default.with("a", true).with("b", true).with("int", 100));
213} 256}
214 257
215test "clap.parse: long" { 258test "clap: long" {
216 const clap = comptime Clap(Options) { 259 const clap = comptime Clap(Options) {
217 .defaults = default, 260 .defaults = default,
218 .params = []Param { 261 .params = []Param {
219 Param.init("cc"), 262 Param.smart("cc"),
220 Param.init("int").with("takes_value", Parser.int(i64, 10)), 263 Param.smart("int").with("takes_value", Parser.int(i64, 10)),
221 Param.init("uint").with("takes_value", Parser.int(u64, 10)), 264 Param.smart("uint").with("takes_value", Parser.int(u64, 10)),
222 Param.init("str").with("takes_value", Parser.string), 265 Param.smart("str").with("takes_value", Parser.string),
223 } 266 }
224 }; 267 };
225 268
@@ -227,45 +270,45 @@ test "clap.parse: long" {
227 testNoErr(clap, [][]const u8 { "--int", "100" }, default.with("int", 100)); 270 testNoErr(clap, [][]const u8 { "--int", "100" }, default.with("int", 100));
228} 271}
229 272
230test "clap.parse: value bool" { 273test "clap: value bool" {
231 const clap = comptime Clap(Options) { 274 const clap = comptime Clap(Options) {
232 .defaults = default, 275 .defaults = default,
233 .params = []Param { 276 .params = []Param {
234 Param.init("a"), 277 Param.smart("a"),
235 } 278 }
236 }; 279 };
237 280
238 testNoErr(clap, [][]const u8 { "-a" }, default.with("a", true)); 281 testNoErr(clap, [][]const u8 { "-a" }, default.with("a", true));
239} 282}
240 283
241test "clap.parse: value str" { 284test "clap: value str" {
242 const clap = comptime Clap(Options) { 285 const clap = comptime Clap(Options) {
243 .defaults = default, 286 .defaults = default,
244 .params = []Param { 287 .params = []Param {
245 Param.init("str").with("takes_value", Parser.string), 288 Param.smart("str").with("takes_value", Parser.string),
246 } 289 }
247 }; 290 };
248 291
249 testNoErr(clap, [][]const u8 { "--str", "Hello World!" }, default.with("str", "Hello World!")); 292 testNoErr(clap, [][]const u8 { "--str", "Hello World!" }, default.with("str", "Hello World!"));
250} 293}
251 294
252test "clap.parse: value int" { 295test "clap: value int" {
253 const clap = comptime Clap(Options) { 296 const clap = comptime Clap(Options) {
254 .defaults = default, 297 .defaults = default,
255 .params = []Param { 298 .params = []Param {
256 Param.init("int").with("takes_value", Parser.int(i64, 10)), 299 Param.smart("int").with("takes_value", Parser.int(i64, 10)),
257 } 300 }
258 }; 301 };
259 302
260 testNoErr(clap, [][]const u8 { "--int", "100" }, default.with("int", 100)); 303 testNoErr(clap, [][]const u8 { "--int", "100" }, default.with("int", 100));
261} 304}
262 305
263test "clap.parse: position" { 306test "clap: position" {
264 const clap = comptime Clap(Options) { 307 const clap = comptime Clap(Options) {
265 .defaults = default, 308 .defaults = default,
266 .params = []Param { 309 .params = []Param {
267 Param.init("a").with("position", 0), 310 Param.smart("a").with("position", 0),
268 Param.init("b").with("position", 1), 311 Param.smart("b").with("position", 1),
269 } 312 }
270 }; 313 };
271 314