shithub: mc

Download patch

ref: db3ef18d59af1f528cb4b12f9075396773b6dca4
parent: 16f5afe322e39f1716716700188664305b829d59
author: Ori Bernstein <[email protected]>
date: Wed Oct 24 12:00:19 EDT 2012

Start supporting option parsing.

--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -30,7 +30,7 @@
 start.s: start-$(SYS).s
 	cp start-$(SYS).s start.s
 
-test: libstd.a
+test: libstd.a test.myr
 	../myrbuild/myrbuild -b test -I. test.myr
 
 
--- a/libstd/option.myr
+++ b/libstd/option.myr
@@ -1,7 +1,7 @@
 use "types.use"
 use "alloc.use"
 use "utf.use"
-use "fmt.use"
+use "die.use"
 
 pkg std =
 	type optctx = struct
@@ -10,7 +10,7 @@
 		args	: byte[:][:]
 
 		/* state */
-		optidx	: size
+		argidx	: size
 		curarg	: byte[:]
 		arglist	: byte[:][:]
 	;;
@@ -17,6 +17,7 @@
 
 	const optinit	: (opts	: byte[:], opts : byte[:][:] -> optctx*)
 	const optnext	: (ctx : optctx* -> char)
+	const optarg	: (ctx : optctx* -> byte[:])
 ;;
 
 const optinit = {opts, args
@@ -25,7 +26,7 @@
 	ctx = alloc()
 	ctx.opts = opts
 	ctx.args = args
-	ctx.optidx = 0
+	ctx.argidx = 0
 	ctx.arglist = [][:]
 	nextopt(ctx)
 	-> ctx
@@ -43,10 +44,26 @@
 	-> 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.optidx + 1; i < ctx.args.len; i++
+	for i = ctx.argidx + 1; i < ctx.args.len; i++
 		if decode(ctx.args[i]) == '-'
 			goto foundopt
 		else
@@ -56,7 +73,7 @@
 	;;
 	-> false
 :foundopt
-	ctx.optidx = i
+	ctx.argidx = i
 	ctx.curarg = ctx.args[i][1:]
 	-> true
 }
--- a/libstd/test.myr
+++ b/libstd/test.myr
@@ -15,6 +15,10 @@
         opt = std.optinit("asdf:", args)
         while (o = std.optnext(opt)) != std.Badchar
         	std.put("option %c\n", o)
+		match o
+		'a':	std.put("\targ=%s\n", std.optarg(opt));;
+		'b':	std.put("\targ=%s\n", std.optarg(opt));;
+		;;
         ;;