summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-14 21:34:16 -0700
committerOri Bernstein <ori@eigenstate.org>2015-06-15 01:19:24 -0700
commita32760e295c8d919349e4ee39661b66195a938c8 (patch)
treed5507445972f7bd8a909d873c47ba92f39d523ab /libstd
parent94acc736ec2d8d80533439a24742d7c796aaab4f (diff)
downloadmc-a32760e295c8d919349e4ee39661b66195a938c8.tar.gz
Add maxargs, noargs options to optparse.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/optparse.myr12
1 files changed, 12 insertions, 0 deletions
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index cd22801..9d5cdbb 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -14,6 +14,8 @@ pkg std =
type optdef = struct
argdesc : byte[:] /* the description for the usage */
minargs : std.size /* the minimum number of positional args */
+ maxargs : std.size /* the maximum number of positional args (0 = unlimited) */
+ noargs : std.bool /* whether we accept args at all */
opts : optdesc[:] /* the description of the options */
;;
@@ -66,6 +68,16 @@ const optparse = {args, def
optusage(ctx.optargs[0], ctx.optdef)
exit(1)
;;
+ if def.maxargs > 0 && ctx.args.len < def.minargs
+ put("error: expected at most {} args, got {}\n", def.minargs, ctx.args.len)
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(1)
+ ;;
+ if def.noargs && ctx.args.len != 0
+ put("error: expected no args, got {}\n", ctx.args.len)
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(1)
+ ;;
parsed.args = ctx.args
-> parsed
}