ref: a32760e295c8d919349e4ee39661b66195a938c8
parent: 94acc736ec2d8d80533439a24742d7c796aaab4f
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jun 14 17:34:16 EDT 2015
Add maxargs, noargs options to optparse.
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -14,6 +14,8 @@
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 */
;;
@@ -63,6 +65,16 @@
;;
if ctx.args.len < def.minargs
put("error: expected at least {} args, got {}\n", def.minargs, ctx.args.len)
+ 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)
;;