diff options
| -rw-r--r-- | clap.zig | 25 | ||||
| -rw-r--r-- | test.zig | 54 |
2 files changed, 27 insertions, 52 deletions
| @@ -210,7 +210,7 @@ pub const OsArgIterator = struct { | |||
| 210 | /// ::StreamingClap.next to parse all the arguments of your program. | 210 | /// ::StreamingClap.next to parse all the arguments of your program. |
| 211 | pub fn StreamingClap(comptime Id: type, comptime ArgError: type) type { | 211 | pub fn StreamingClap(comptime Id: type, comptime ArgError: type) type { |
| 212 | return struct { | 212 | return struct { |
| 213 | const Self = @This(); | 213 | const Self = @This(); |
| 214 | 214 | ||
| 215 | const State = union(enum) { | 215 | const State = union(enum) { |
| 216 | Normal, | 216 | Normal, |
| @@ -374,26 +374,3 @@ pub fn StreamingClap(comptime Id: type, comptime ArgError: type) type { | |||
| 374 | } | 374 | } |
| 375 | }; | 375 | }; |
| 376 | } | 376 | } |
| 377 | |||
| 378 | pub fn ToStructId(comptime T: type) type { | ||
| 379 | return struct { | ||
| 380 | parse: fn(*T, ?[]const u8) | ||
| 381 | }; | ||
| 382 | } | ||
| 383 | |||
| 384 | const ToStructParamError = error{}; | ||
| 385 | const ToStructParam = Param(fn (*T, ?[]const u8) ToStructParamError!void); | ||
| 386 | |||
| 387 | fn paramsFromStruct(comptime T: type) []const ToStructParam { | ||
| 388 | var res: []const ToStructParam = []ToStructParam{}; | ||
| 389 | |||
| 390 | for (@typeInfo(T).Struct.fields) |field| { | ||
| 391 | res = res ++ []ToStructParam{ | ||
| 392 | ToStructParam.init() | ||
| 393 | }; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | pub fn toStruct(defaults: var, iter: *ArgIterator(ArgError)) !@typeOf(defaults) { | ||
| 398 | |||
| 399 | } | ||
| @@ -11,7 +11,7 @@ const Names = clap.Names; | |||
| 11 | const Param = clap.Param(u8); | 11 | const Param = clap.Param(u8); |
| 12 | const StreamingClap = clap.StreamingClap(u8, ArgSliceIterator.Error); | 12 | const StreamingClap = clap.StreamingClap(u8, ArgSliceIterator.Error); |
| 13 | const Arg = clap.Arg(u8); | 13 | const Arg = clap.Arg(u8); |
| 14 | 14 | ||
| 15 | fn testNoErr(params: []const Param, args: []const []const u8, results: []const Arg) void { | 15 | fn testNoErr(params: []const Param, args: []const []const u8, results: []const Arg) void { |
| 16 | var arg_iter = ArgSliceIterator.init(args); | 16 | var arg_iter = ArgSliceIterator.init(args); |
| 17 | var c = StreamingClap.init(params, &arg_iter.iter); | 17 | var c = StreamingClap.init(params, &arg_iter.iter); |
| @@ -26,7 +26,7 @@ fn testNoErr(params: []const Param, args: []const []const u8, results: []const A | |||
| 26 | const actual_value = arg.value orelse unreachable; | 26 | const actual_value = arg.value orelse unreachable; |
| 27 | debug.assert(mem.eql(u8, expected_value, actual_value)); | 27 | debug.assert(mem.eql(u8, expected_value, actual_value)); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | if (c.next() catch unreachable) |_| { | 30 | if (c.next() catch unreachable) |_| { |
| 31 | unreachable; | 31 | unreachable; |
| 32 | } | 32 | } |
| @@ -38,17 +38,17 @@ test "clap: short" { | |||
| 38 | Param.init(1, false, Names.short('b')), | 38 | Param.init(1, false, Names.short('b')), |
| 39 | Param.init(2, true, Names.short('c')), | 39 | Param.init(2, true, Names.short('c')), |
| 40 | }; | 40 | }; |
| 41 | 41 | ||
| 42 | const a = ¶ms[0]; | 42 | const a = ¶ms[0]; |
| 43 | const b = ¶ms[1]; | 43 | const b = ¶ms[1]; |
| 44 | const c = ¶ms[2]; | 44 | const c = ¶ms[2]; |
| 45 | 45 | ||
| 46 | testNoErr( | 46 | testNoErr( |
| 47 | params, | 47 | params, |
| 48 | [][]const u8{ | 48 | [][]const u8{ |
| 49 | "-a", "-b", "-ab", "-ba", | 49 | "-a", "-b", "-ab", "-ba", |
| 50 | "-c", "0", "-c=0", | 50 | "-c", "0", "-c=0", "-ac", |
| 51 | "-ac", "0", "-ac=0", | 51 | "0", "-ac=0", |
| 52 | }, | 52 | }, |
| 53 | []const Arg{ | 53 | []const Arg{ |
| 54 | Arg.init(a, null), | 54 | Arg.init(a, null), |
| @@ -73,16 +73,17 @@ test "clap: long" { | |||
| 73 | Param.init(1, false, Names.long("bb")), | 73 | Param.init(1, false, Names.long("bb")), |
| 74 | Param.init(2, true, Names.long("cc")), | 74 | Param.init(2, true, Names.long("cc")), |
| 75 | }; | 75 | }; |
| 76 | 76 | ||
| 77 | const aa = ¶ms[0]; | 77 | const aa = ¶ms[0]; |
| 78 | const bb = ¶ms[1]; | 78 | const bb = ¶ms[1]; |
| 79 | const cc = ¶ms[2]; | 79 | const cc = ¶ms[2]; |
| 80 | 80 | ||
| 81 | testNoErr( | 81 | testNoErr( |
| 82 | params, | 82 | params, |
| 83 | [][]const u8{ | 83 | [][]const u8{ |
| 84 | "--aa", "--bb", | 84 | "--aa", "--bb", |
| 85 | "--cc", "0", "--cc=0", | 85 | "--cc", "0", |
| 86 | "--cc=0", | ||
| 86 | }, | 87 | }, |
| 87 | []const Arg{ | 88 | []const Arg{ |
| 88 | Arg.init(aa, null), | 89 | Arg.init(aa, null), |
| @@ -99,16 +100,17 @@ test "clap: bare" { | |||
| 99 | Param.init(1, false, Names.bare("bb")), | 100 | Param.init(1, false, Names.bare("bb")), |
| 100 | Param.init(2, true, Names.bare("cc")), | 101 | Param.init(2, true, Names.bare("cc")), |
| 101 | }; | 102 | }; |
| 102 | 103 | ||
| 103 | const aa = ¶ms[0]; | 104 | const aa = ¶ms[0]; |
| 104 | const bb = ¶ms[1]; | 105 | const bb = ¶ms[1]; |
| 105 | const cc = ¶ms[2]; | 106 | const cc = ¶ms[2]; |
| 106 | 107 | ||
| 107 | testNoErr( | 108 | testNoErr( |
| 108 | params, | 109 | params, |
| 109 | [][]const u8{ | 110 | [][]const u8{ |
| 110 | "aa", "bb", | 111 | "aa", "bb", |
| 111 | "cc", "0", "cc=0", | 112 | "cc", "0", |
| 113 | "cc=0", | ||
| 112 | }, | 114 | }, |
| 113 | []const Arg{ | 115 | []const Arg{ |
| 114 | Arg.init(aa, null), | 116 | Arg.init(aa, null), |
| @@ -120,13 +122,11 @@ test "clap: bare" { | |||
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | test "clap: none" { | 124 | test "clap: none" { |
| 123 | const params = []Param{ | 125 | const params = []Param{Param.init(0, true, Names.none())}; |
| 124 | Param.init(0, true, Names.none()), | 126 | |
| 125 | }; | ||
| 126 | |||
| 127 | testNoErr( | 127 | testNoErr( |
| 128 | params, | 128 | params, |
| 129 | [][]const u8{"aa", "bb"}, | 129 | [][]const u8{ "aa", "bb" }, |
| 130 | []const Arg{ | 130 | []const Arg{ |
| 131 | Arg.init(¶ms[0], "aa"), | 131 | Arg.init(¶ms[0], "aa"), |
| 132 | Arg.init(¶ms[0], "bb"), | 132 | Arg.init(¶ms[0], "bb"), |
| @@ -153,22 +153,20 @@ test "clap: all" { | |||
| 153 | }), | 153 | }), |
| 154 | Param.init(3, true, Names.none()), | 154 | Param.init(3, true, Names.none()), |
| 155 | }; | 155 | }; |
| 156 | 156 | ||
| 157 | const aa = ¶ms[0]; | 157 | const aa = ¶ms[0]; |
| 158 | const bb = ¶ms[1]; | 158 | const bb = ¶ms[1]; |
| 159 | const cc = ¶ms[2]; | 159 | const cc = ¶ms[2]; |
| 160 | const bare = ¶ms[3]; | 160 | const bare = ¶ms[3]; |
| 161 | 161 | ||
| 162 | testNoErr( | 162 | testNoErr( |
| 163 | params, | 163 | params, |
| 164 | [][]const u8{ | 164 | [][]const u8{ |
| 165 | "-a", "-b", "-ab", "-ba", | 165 | "-a", "-b", "-ab", "-ba", |
| 166 | "-c", "0", "-c=0", | 166 | "-c", "0", "-c=0", "-ac", |
| 167 | "-ac", "0", "-ac=0", | 167 | "0", "-ac=0", "--aa", "--bb", |
| 168 | "--aa", "--bb", | 168 | "--cc", "0", "--cc=0", "aa", |
| 169 | "--cc", "0", "--cc=0", | 169 | "bb", "cc", "0", "cc=0", |
| 170 | "aa", "bb", | ||
| 171 | "cc", "0", "cc=0", | ||
| 172 | "something", | 170 | "something", |
| 173 | }, | 171 | }, |
| 174 | []const Arg{ | 172 | []const Arg{ |
| @@ -217,9 +215,9 @@ test "clap.Example" { | |||
| 217 | const params = []c.Param(u8){ | 215 | const params = []c.Param(u8){ |
| 218 | c.Param(u8).init('h', false, c.Names.prefix("help")), | 216 | c.Param(u8).init('h', false, c.Names.prefix("help")), |
| 219 | c.Param(u8).init('v', false, c.Names.prefix("version")), | 217 | c.Param(u8).init('v', false, c.Names.prefix("version")), |
| 220 | c.Param(u8).init('f', true, c.Names.none()), | 218 | c.Param(u8).init('f', true, c.Names.none()), |
| 221 | }; | 219 | }; |
| 222 | 220 | ||
| 223 | // Here, we use an `ArgSliceIterator` which iterates over | 221 | // Here, we use an `ArgSliceIterator` which iterates over |
| 224 | // a slice of arguments. For real program, you would probably | 222 | // a slice of arguments. For real program, you would probably |
| 225 | // use `OsArgIterator`. | 223 | // use `OsArgIterator`. |
| @@ -244,4 +242,4 @@ test "clap.Example" { | |||
| 244 | else => unreachable, | 242 | else => unreachable, |
| 245 | } | 243 | } |
| 246 | } | 244 | } |
| 247 | } \ No newline at end of file | 245 | } |