shithub: limbobyexample

Download patch

ref: 169cceaef46664b809cc13f52f9cfaa88275d610
parent: 23c8f3f9409d2826a94277464125872319156911
author: seh <henesy.dev@gmail.com>
date: Mon Mar 18 12:15:05 EDT 2019

work on args.m

--- /dev/null
+++ b/Args/README.md
@@ -1,0 +1,50 @@
+# Command-Line Arguments
+
+Inferno has a dedicated module for processing commandline flags and arguments, arg(2). 
+
+## Source
+
+### args.b:17,22
+
+
+
+### args.b:24,37
+
+
+
+Note: After the processing of all flags, `argv` is re-set from the value contained within `arg` to remove the elements (if any) utilized by flags and their arguments. 
+
+### args.b:41,51
+
+
+
+## Demo
+
+	; limbo args.b
+	; args -h
+	usage: args [-r] [-c mark] words...
+	; args a b c d
+	Argc after flags: 4
+	→  a
+	→  b
+	→  c
+	→  d
+	; args -r a b c d
+	Argc after flags: 4
+	→  d
+	→  c
+	→  b
+	→  a
+	; args -c 'quack: ' a b c d
+	Argc after flags: 4
+	quack:  a
+	quack:  b
+	quack:  c
+	quack:  d
+	; args -r -c '-> ' d c b a
+	Argc after flags: 4
+	->  a
+	->  b
+	->  c
+	->  d
+	; 
--- /dev/null
+++ b/Args/args.b
@@ -1,0 +1,54 @@
+implement Args;
+
+include "sys.m";
+include "draw.m";
+
+include "arg.m";
+
+sys: Sys;
+print: import sys;
+
+Args: module {
+	init: fn(nil: ref Draw->Context, argv: list of string);
+};
+
+init(nil: ref Draw->Context, argv: list of string) {
+	sys = load Sys Sys->PATH;
+	arg := load Arg Arg->PATH;
+	if(arg == nil)
+		raise "bad module load";
+
+	arg->init(argv);
+	arg->setusage(arg->progname() + " [-r] [-c mark] words...");
+
+	rev := 0;
+	mark := "→ ";
+
+	while((opt := arg->opt()) != 0)
+		case opt {
+		'r' =>
+			rev = 1;
+		'c' =>
+			mark = arg->earg();
+		* =>
+			arg->usage();
+		}
+
+	argv = arg->argv();
+
+	print("Argc after flags: %d\n", len argv);
+
+	if(rev) {
+		argl: list of string;
+
+		for(; argv != nil; argv = tl argv)
+			argl = hd argv :: argl;
+
+		argv = argl;
+	}
+
+	for(; argv != nil; argv = tl argv)
+		sys->print("%s %s\n", mark, hd argv);
+
+	exit;
+}