ref: f61764f57d5b9b8e9e72a3c7eb1eead702cf3108
parent: 4d3579d9e03400cc44fca47638454207c86b9fd2
author: Ori Bernstein <[email protected]>
date: Wed Jan 16 11:33:34 EST 2013
Rename the command line option parser to 'optparse'. Option sounds too much like the implementation of option types.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -6,7 +6,7 @@
extremum.myr \
fmt.myr \
maybe.myr \
- option.myr \
+ optparse.myr \
rand.myr \
slurp.myr \
sys.myr \
--- a/libstd/option.myr
+++ /dev/null
@@ -1,79 +1,0 @@
-use "types.use"
-use "alloc.use"
-use "utf.use"
-use "die.use"
-
-pkg std =
- type optctx = struct
- /* data passed in */
- opts : byte[:]
- args : byte[:][:]
-
- /* state */
- argidx : size
- curarg : byte[:]
- arglist : byte[:][:]
- ;;
-
- const optinit : (opts : byte[:], opts : byte[:][:] -> optctx*)
- const optnext : (ctx : optctx* -> char)
- const optarg : (ctx : optctx* -> byte[:])
-;;
-
-const optinit = {opts, args
- var ctx
-
- ctx = alloc()
- ctx.opts = opts
- ctx.args = args
- ctx.argidx = 0
- ctx.arglist = [][:]
- nextopt(ctx)
- -> ctx
-}
-
-const optnext = {ctx
- var c
-
- if !ctx.curarg.len
- if !nextopt(ctx)
- -> Badchar
- ;;
- ;;
- (c, ctx.curarg) = striter(ctx.curarg)
- -> c
-}
-
-const optarg = {ctx
- var arg
-
- if ctx.curarg.len > 0
- arg = ctx.curarg
- ctx.curarg = ctx.curarg[ctx.curarg.len:]
- elif ctx.argidx > ctx.args.len
- arg = ctx.args[ctx.argidx + 1]
- ctx.argidx++
- nextopt(ctx)
- else
- die("Arg needed")
- ;;
- -> arg
-}
-
-const nextopt = {ctx
- var i
-
- for i = ctx.argidx + 1; i < ctx.args.len; i++
- if decode(ctx.args[i]) == '-'
- goto foundopt
- else
- /* FIXME: implement slappend */
- /* ctx.args = slappend(ctx.args, ctx.args[i]) */
- ;;
- ;;
- -> false
-:foundopt
- ctx.argidx = i
- ctx.curarg = ctx.args[i][1:]
- -> true
-}
--- /dev/null
+++ b/libstd/optparse.myr
@@ -1,0 +1,79 @@
+use "types.use"
+use "alloc.use"
+use "utf.use"
+use "die.use"
+
+pkg std =
+ type optctx = struct
+ /* data passed in */
+ opts : byte[:]
+ args : byte[:][:]
+
+ /* state */
+ argidx : size
+ curarg : byte[:]
+ arglist : byte[:][:]
+ ;;
+
+ const optinit : (opts : byte[:], opts : byte[:][:] -> optctx*)
+ const optnext : (ctx : optctx* -> char)
+ const optarg : (ctx : optctx* -> byte[:])
+;;
+
+const optinit = {opts, args
+ var ctx
+
+ ctx = alloc()
+ ctx.opts = opts
+ ctx.args = args
+ ctx.argidx = 0
+ ctx.arglist = [][:]
+ nextopt(ctx)
+ -> ctx
+}
+
+const optnext = {ctx
+ var c
+
+ if !ctx.curarg.len
+ if !nextopt(ctx)
+ -> Badchar
+ ;;
+ ;;
+ (c, ctx.curarg) = striter(ctx.curarg)
+ -> c
+}
+
+const optarg = {ctx
+ var arg
+
+ if ctx.curarg.len > 0
+ arg = ctx.curarg
+ ctx.curarg = ctx.curarg[ctx.curarg.len:]
+ elif ctx.argidx > ctx.args.len
+ arg = ctx.args[ctx.argidx + 1]
+ ctx.argidx++
+ nextopt(ctx)
+ else
+ die("Arg needed")
+ ;;
+ -> arg
+}
+
+const nextopt = {ctx
+ var i
+
+ for i = ctx.argidx + 1; i < ctx.args.len; i++
+ if decode(ctx.args[i]) == '-'
+ goto foundopt
+ else
+ /* FIXME: implement slappend */
+ /* ctx.args = slappend(ctx.args, ctx.args[i]) */
+ ;;
+ ;;
+ -> false
+:foundopt
+ ctx.argidx = i
+ ctx.curarg = ctx.args[i][1:]
+ -> true
+}