summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-05-02 19:56:20 -0700
committerOri Bernstein <ori@eigenstate.org>2015-05-02 19:56:20 -0700
commitb08f20e577c31f439f4beb525125c6823b27c606 (patch)
tree0d2e46a3ba7d5576364a11e028d4166c3deedc18
parentdaa3e93b17d5a3ba192da6b3a579fa00930b46dd (diff)
downloadmc-b08f20e577c31f439f4beb525125c6823b27c606.tar.gz
Add std.optusage() function.
Now you can show usage manually if needed.
-rw-r--r--libstd/optparse.myr15
1 files changed, 8 insertions, 7 deletions
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index 06b2093..768d666 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -30,6 +30,7 @@ pkg std =
;;
const optparse : (optargs : byte[:][:], def : optdef# -> optparsed)
+ const optusage : (prog : byte[:], def : optdef# -> void)
;;
type optctx = struct
@@ -58,7 +59,7 @@ const optparse = {args, def
;;
if ctx.args.len < def.minargs
put("error: expected at least %z args, got %z\n", def.minargs, ctx.args.len)
- optusage(&ctx)
+ optusage(ctx.optargs[0], ctx.optdef)
exit(1)
;;
parsed.args = ctx.args
@@ -89,7 +90,7 @@ const optnext = {ctx
match optinfo(ctx, c)
| `None:
if c == 'h' || c == '?'
- optusage(ctx)
+ optusage(ctx.optargs[0], ctx.optdef)
exit(0)
else
fatal(1, "unexpected argument '%c'\n", c)
@@ -156,15 +157,15 @@ const next = {ctx
-> true
}
-const optusage = {ctx
- std.put("usage: %s [-h?]", ctx.optargs[0])
- for o in ctx.optdef.opts
+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", ctx.optdef.argdesc)
+ 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 ctx.optdef.opts
+ for o in def.opts
std.put("\t-%c%s%s\t%s\n", o.opt, sep(o.arg), o.arg, o.desc)
;;
}