diff options
| author | 2018-06-01 09:07:47 +0200 | |
|---|---|---|
| committer | 2018-06-01 09:07:47 +0200 | |
| commit | 5ebe2be10b96410a6182a2aa66ff0d2bd43eed3f (patch) | |
| tree | 9d6da79c50189aa92d12ba3d68ad2a1eff74cee6 /examples/core.zig | |
| parent | Id is now gotten through param of arg (diff) | |
| download | zig-clap-5ebe2be10b96410a6182a2aa66ff0d2bd43eed3f.tar.gz zig-clap-5ebe2be10b96410a6182a2aa66ff0d2bd43eed3f.tar.xz zig-clap-5ebe2be10b96410a6182a2aa66ff0d2bd43eed3f.zip | |
The arg iterator is now responsible for allocation instead of core.Clap
Diffstat (limited to 'examples/core.zig')
| -rw-r--r-- | examples/core.zig | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/examples/core.zig b/examples/core.zig index 8bcc928..eb512ef 100644 --- a/examples/core.zig +++ b/examples/core.zig | |||
| @@ -7,8 +7,10 @@ const mem = std.mem; | |||
| 7 | const Names = clap.Names; | 7 | const Names = clap.Names; |
| 8 | const Param = clap.Param; | 8 | const Param = clap.Param; |
| 9 | 9 | ||
| 10 | const ArgError = clap.OsArgIterator.Error; | ||
| 11 | |||
| 10 | // TODO: More specific error in this func type. | 12 | // TODO: More specific error in this func type. |
| 11 | const Command = fn(&mem.Allocator, &clap.ArgIterator) error!void; | 13 | const Command = fn(&mem.Allocator, &clap.ArgIterator(ArgError)) error!void; |
| 12 | 14 | ||
| 13 | const params = []Param(Command){ | 15 | const params = []Param(Command){ |
| 14 | Param(Command).init(help, false, Names.prefix("help")), | 16 | Param(Command).init(help, false, Names.prefix("help")), |
| @@ -52,11 +54,12 @@ pub fn main() !void { | |||
| 52 | 54 | ||
| 53 | const allocator = &arena.allocator; | 55 | const allocator = &arena.allocator; |
| 54 | 56 | ||
| 55 | var args = clap.OsArgIterator.init(); | 57 | var args = clap.OsArgIterator.init(allocator); |
| 56 | var parser = clap.Clap(Command).init(params, &args.iter, allocator); | 58 | defer args.deinit(); |
| 57 | defer parser.deinit(); | 59 | |
| 60 | const exe = try args.iter.next(); | ||
| 61 | var parser = clap.Clap(Command, ArgError).init(params, &args.iter); | ||
| 58 | 62 | ||
| 59 | const exe = try parser.nextNoParse(); | ||
| 60 | const maybe_arg = parser.next() catch |err| b: { | 63 | const maybe_arg = parser.next() catch |err| b: { |
| 61 | debug.warn("{}.\n", @errorName(err)); | 64 | debug.warn("{}.\n", @errorName(err)); |
| 62 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches | 65 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches |
| @@ -71,7 +74,7 @@ pub fn main() !void { | |||
| 71 | try arg.param.id(allocator, parser.iter); | 74 | try arg.param.id(allocator, parser.iter); |
| 72 | } | 75 | } |
| 73 | 76 | ||
| 74 | pub fn help(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 77 | pub fn help(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 75 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches | 78 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches |
| 76 | } | 79 | } |
| 77 | 80 | ||
| @@ -144,7 +147,7 @@ const missing_build_file = | |||
| 144 | \\ | 147 | \\ |
| 145 | ; | 148 | ; |
| 146 | 149 | ||
| 147 | fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator) !void { | 150 | fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) !void { |
| 148 | var init = false; | 151 | var init = false; |
| 149 | var build_file: []const u8 = "build.zig"; | 152 | var build_file: []const u8 = "build.zig"; |
| 150 | var cache_dir: []const u8 = "zig-cache"; | 153 | var cache_dir: []const u8 = "zig-cache"; |
| @@ -157,8 +160,7 @@ fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator) !void { | |||
| 157 | var verbose_llvm_ir = false; | 160 | var verbose_llvm_ir = false; |
| 158 | var verbose_cimport = false; | 161 | var verbose_cimport = false; |
| 159 | 162 | ||
| 160 | var parser = clap.Clap(Build).init(build_params, args, allocator); | 163 | var parser = clap.Clap(Build, ArgError).init(build_params, args); |
| 161 | defer parser.deinit(); | ||
| 162 | 164 | ||
| 163 | while (parser.next() catch |err| { | 165 | while (parser.next() catch |err| { |
| 164 | debug.warn("{}.\n", @errorName(err)); | 166 | debug.warn("{}.\n", @errorName(err)); |
| @@ -321,17 +323,17 @@ const build_generic_usage = | |||
| 321 | ; | 323 | ; |
| 322 | 324 | ||
| 323 | 325 | ||
| 324 | fn cmdBuildExe(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 326 | fn cmdBuildExe(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 325 | } | 327 | } |
| 326 | 328 | ||
| 327 | // cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// | 329 | // cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// |
| 328 | 330 | ||
| 329 | fn cmdBuildLib(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 331 | fn cmdBuildLib(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 330 | } | 332 | } |
| 331 | 333 | ||
| 332 | // cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// | 334 | // cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// |
| 333 | 335 | ||
| 334 | fn cmdBuildObj(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 336 | fn cmdBuildObj(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 335 | } | 337 | } |
| 336 | 338 | ||
| 337 | // cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// | 339 | // cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// |
| @@ -348,17 +350,17 @@ const usage_fmt = | |||
| 348 | \\ | 350 | \\ |
| 349 | ; | 351 | ; |
| 350 | 352 | ||
| 351 | fn cmdFmt(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 353 | fn cmdFmt(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 352 | } | 354 | } |
| 353 | 355 | ||
| 354 | // cmd:targets ///////////////////////////////////////////////////////////////////////////////////// | 356 | // cmd:targets ///////////////////////////////////////////////////////////////////////////////////// |
| 355 | 357 | ||
| 356 | fn cmdTargets(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 358 | fn cmdTargets(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 357 | } | 359 | } |
| 358 | 360 | ||
| 359 | // cmd:version ///////////////////////////////////////////////////////////////////////////////////// | 361 | // cmd:version ///////////////////////////////////////////////////////////////////////////////////// |
| 360 | 362 | ||
| 361 | fn cmdVersion(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 363 | fn cmdVersion(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 362 | } | 364 | } |
| 363 | 365 | ||
| 364 | // cmd:test //////////////////////////////////////////////////////////////////////////////////////// | 366 | // cmd:test //////////////////////////////////////////////////////////////////////////////////////// |
| @@ -372,7 +374,7 @@ const usage_test = | |||
| 372 | \\ | 374 | \\ |
| 373 | ; | 375 | ; |
| 374 | 376 | ||
| 375 | fn cmdTest(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 377 | fn cmdTest(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 376 | } | 378 | } |
| 377 | 379 | ||
| 378 | // cmd:run ///////////////////////////////////////////////////////////////////////////////////////// | 380 | // cmd:run ///////////////////////////////////////////////////////////////////////////////////////// |
| @@ -388,7 +390,7 @@ const usage_run = | |||
| 388 | \\ | 390 | \\ |
| 389 | ; | 391 | ; |
| 390 | 392 | ||
| 391 | fn cmdRun(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 393 | fn cmdRun(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 392 | } | 394 | } |
| 393 | 395 | ||
| 394 | // cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// | 396 | // cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// |
| @@ -404,7 +406,7 @@ const usage_translate_c = | |||
| 404 | \\ | 406 | \\ |
| 405 | ; | 407 | ; |
| 406 | 408 | ||
| 407 | fn cmdTranslateC(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 409 | fn cmdTranslateC(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 408 | } | 410 | } |
| 409 | 411 | ||
| 410 | // cmd:zen ///////////////////////////////////////////////////////////////////////////////////////// | 412 | // cmd:zen ///////////////////////////////////////////////////////////////////////////////////////// |
| @@ -426,7 +428,7 @@ const info_zen = | |||
| 426 | \\ | 428 | \\ |
| 427 | ; | 429 | ; |
| 428 | 430 | ||
| 429 | fn cmdZen(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 431 | fn cmdZen(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 430 | } | 432 | } |
| 431 | 433 | ||
| 432 | // cmd:internal //////////////////////////////////////////////////////////////////////////////////// | 434 | // cmd:internal //////////////////////////////////////////////////////////////////////////////////// |
| @@ -440,5 +442,5 @@ const usage_internal = | |||
| 440 | \\ | 442 | \\ |
| 441 | ; | 443 | ; |
| 442 | 444 | ||
| 443 | fn cmdInternal(allocator: &mem.Allocator, args: &clap.ArgIterator) (error{}!void) { | 445 | fn cmdInternal(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { |
| 444 | } | 446 | } |