ref: 5519708b4c280661204744f50e140298919dd718
parent: c6dc304c9f9bf6e20f0f1b36a961115573e2aecd
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Nov 16 08:34:28 EST 2016
Allow optional capturing of CLI args directly. No need to iterate to get the arg value.
--- a/lib/std/optparse.myr
+++ b/lib/std/optparse.myr
@@ -24,6 +24,7 @@
opt : char
arg : byte[:]
desc : byte[:]
+ dest : std.option(byte[:]#)
optional : bool
;;
@@ -114,7 +115,7 @@
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 @@
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 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