summaryrefslogtreecommitdiff
path: root/libstd/optparse.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-01-10 18:28:45 -0500
committerOri Bernstein <ori@eigenstate.org>2014-01-10 18:28:45 -0500
commit427b9849fae0c94709a2ae3a43e17d5fe938ecc6 (patch)
tree3c17a137d1f2814443f1310c65871920abaa6e1a /libstd/optparse.myr
parent9a2785cb9f3cd47fbe06371d4fc982d5f0ae7120 (diff)
downloadmc-427b9849fae0c94709a2ae3a43e17d5fe938ecc6.tar.gz
Fix double-next() problem
This was doubling the args in the args array in some cases (ie, when we had a command line option that took an entire argument).
Diffstat (limited to 'libstd/optparse.myr')
-rw-r--r--libstd/optparse.myr52
1 files changed, 27 insertions, 25 deletions
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index 533d9ee..0da8aae 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -2,6 +2,7 @@ use "alloc.use"
use "die.use"
use "extremum.use"
use "fmt.use"
+use "option.use"
use "slpush.use"
use "sys.use"
use "types.use"
@@ -58,33 +59,34 @@ const optfin = {ctx
const optnext = {ctx
var c
var arg
- var valid
- var tryarg
- var needarg
(c, ctx.curarg) = striter(ctx.curarg)
- (valid, tryarg, needarg) = optinfo(ctx, c)
- if !valid
- put("Unexpected argument %c\n", c)
- exit(1)
- elif tryarg && ctx.curarg.len > 0
- arg = ctx.curarg
- ctx.curarg = ctx.curarg[ctx.curarg.len:]
- elif tryarg && ctx.argidx < (ctx.optargs.len - 1)
- arg = ctx.optargs[ctx.argidx + 1]
- ctx.argidx++
- next(ctx)
- elif needarg
- put("Expected argument for %c\n", c)
- exit(1)
- else
+ match optinfo(ctx, c)
+ | `None:
+ fatal(1, "Unexpected argument %c\n", c)
+ | `Some (true, needed):
+ /* -arg => '-a' 'rg' */
+ if ctx.curarg.len > 0
+ arg = ctx.curarg
+ ctx.curarg = ctx.curarg[ctx.curarg.len:]
+ next(ctx)
+ /* '-a rg' => '-a' 'rg' */
+ elif ctx.argidx < (ctx.optargs.len - 1)
+ arg = ctx.optargs[ctx.argidx + 1]
+ ctx.argidx++
+ next(ctx)
+ elif needed
+ put("Expected argument for %c\n", c)
+ exit(1)
+ ;;
+ | `Some (false, _):
arg = ""
+ if !ctx.curarg.len
+ next(ctx)
+ ;;
;;
- if !ctx.curarg.len
- next(ctx)
- ;;
-> (c, arg)
}
@@ -104,17 +106,17 @@ const optinfo = {ctx, arg
(c, s) = striter(s)
/* mandatory arg */
if c == ':'
- -> (true, true, true)
+ -> `Some (true, true)
/* optional arg */
elif c == '?'
- -> (true, true, false)
+ -> `Some (true, false)
/* no arg */
else
- -> (true, false, false)
+ -> `Some (false, false)
;;
;;
;;
- -> (false, false, false)
+ -> `None
}
const next = {ctx