shithub: mc

ref: b769b88bbc3e0ddcef7991e73b2582d472f08d89
dir: /libstd/optparse.myr/

View raw version
use "alloc.use"
use "die.use"
use "extremum.use"
use "fmt.use"
use "option.use"
use "sleq.use"
use "slpush.use"
use "syswrap-ss.use"
use "syswrap.use"
use "types.use"
use "utf.use"

pkg std =
	type optdef = struct
		argdesc	: byte[:]	/* the description for the usage */
		minargs	: std.size	/* the minimum number of positional args */
		opts	: optdesc[:]	/* the description of the options */
	;;

	type optdesc = struct
		opt	: char
		arg	: byte[:]
		desc	: byte[:]
		optional	: bool
	;;

	type optparsed = struct
		opts	: (char, byte[:])[:]
		args	: byte[:][:]
	;;

	const optparse	: (optargs : byte[:][:], def : optdef# -> optparsed)
	const optusage	: (prog : byte[:], def : optdef# -> void)
;;

type optctx = struct
	/* public variables */
	args	: byte[:][:]

	/* data passed in */
	optdef	: optdef#
	optargs	: byte[:][:]

	/* state */
	optdone	: bool	/* if we've seen '--', everything's an arg */
	finished	: bool	/* if we've processed all the optargs */
	argidx	: size
	curarg	: byte[:]
;;


const optparse = {args, def
	var ctx : optctx
	var parsed

	optinit(&ctx, args, def)
	while !optdone(&ctx)
		parsed.opts = slpush(parsed.opts, optnext(&ctx))
	;;
	if ctx.args.len < def.minargs
		put("error: expected at least %z args, got %z\n", def.minargs, ctx.args.len)
		optusage(ctx.optargs[0], ctx.optdef)
		exit(1)
	;;
	parsed.args = ctx.args
	-> parsed
}

const optinit = {ctx, args, def
	ctx# = [
		.optargs = args,
		.optdef = def,
		.optdone = false,
		.finished = false,
		.argidx = 0,
		.curarg = [][:],
		.args = [][:],
	]

	next(ctx)
	-> ctx
}

const optnext = {ctx
	var c
	var arg

	(c, ctx.curarg) = striter(ctx.curarg)

	match optinfo(ctx, c)
	| `None:
		if c == 'h' || c == '?'
			optusage(ctx.optargs[0], ctx.optdef)
			exit(0)
		else
			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 == 0
			next(ctx)
		;;
	;;


	-> (c, arg)
}

const optdone = {ctx
	-> ctx.curarg.len == 0 && ctx.finished
}

const optinfo = {ctx, opt
	for o in ctx.optdef.opts
		if o.opt == opt
			-> `Some (o.arg.len != 0, !o.optional)
		;;
	;;
	-> `None
}

const next = {ctx
	var i

	for i = ctx.argidx + 1; i < ctx.optargs.len; i++
		if !ctx.optdone && decode(ctx.optargs[i]) == '-'
			if sleq(ctx.optargs[i], "--")
				ctx.optdone = true
			else
				goto foundopt
			;;
		else
			ctx.args = slpush(ctx.args, ctx.optargs[i])
		;;
	;;
:finishedopt
	ctx.finished = true
	-> false
:foundopt
	ctx.argidx = i
	ctx.curarg = ctx.optargs[i][1:]
	-> true
}

const optusage = {prog, def
	std.put("usage: %s [-h?]", prog)
	for o in def.opts
		std.put("[-%c%s%s] ", o.opt, sep(o.arg), o.arg)
	;;
	std.put("%s\n", def.argdesc)
	std.put("\t-h\tprint this help message\n")
	std.put("\t-?\tprint this help message\n")
	for o in def.opts
		std.put("\t-%c%s%s\t%s\n", o.opt, sep(o.arg), o.arg, o.desc)
	;;
}

const sep = {s
	if s.len > 0
		-> " "
	else
		-> ""
	;;
}