summaryrefslogtreecommitdiff
path: root/lib/std/optparse.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-11-16 13:34:28 -0800
committerOri Bernstein <ori@eigenstate.org>2016-11-16 13:34:28 -0800
commit5519708b4c280661204744f50e140298919dd718 (patch)
treed5e371176a431bb7db0594a16f2724af0b39cc10 /lib/std/optparse.myr
parentc6dc304c9f9bf6e20f0f1b36a961115573e2aecd (diff)
downloadmc-5519708b4c280661204744f50e140298919dd718.tar.gz
Allow optional capturing of CLI args directly.
No need to iterate to get the arg value.
Diffstat (limited to 'lib/std/optparse.myr')
-rw-r--r--lib/std/optparse.myr11
1 files changed, 8 insertions, 3 deletions
diff --git a/lib/std/optparse.myr b/lib/std/optparse.myr
index 62def36..4e67dce 100644
--- a/lib/std/optparse.myr
+++ b/lib/std/optparse.myr
@@ -24,6 +24,7 @@ pkg std =
opt : char
arg : byte[:]
desc : byte[:]
+ dest : std.option(byte[:]#)
optional : bool
;;
@@ -114,7 +115,7 @@ const optnext = {ctx
else
fatal("unexpected argument '{}'\n", c)
;;
- | `Some (true, needed):
+ | `Some (true, needed, dest):
/* -arg => '-a' 'rg' */
if ctx.curarg.len > 0
arg = ctx.curarg
@@ -129,7 +130,11 @@ const optnext = {ctx
put("Expected argument for {}\n", c)
exit(1)
;;
- | `Some (false, _):
+ match dest
+ | `std.Some d: d# = arg
+ | `std.None: /* nothing */
+ ;;
+ | `Some (false, _, _):
arg = ""
if ctx.curarg.len == 0
next(ctx)
@@ -147,7 +152,7 @@ const optdone = {ctx
const optinfo = {ctx, opt
for o in ctx.optdef.opts
if o.opt == opt
- -> `Some (o.arg.len != 0, !o.optional)
+ -> `Some (o.arg.len != 0, !o.optional, o.dest)
;;
;;
-> `None