diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-11-16 16:28:03 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-11-18 07:05:52 +0100 |
commit | b44a4bae7d5c1098709dfaffd122263e0f4c5bc6 (patch) | |
tree | 2789f5ab99317bf1e399f82b173e7d2ba3b1ce1c /lib/getopt.tes | |
parent | cb5e08b40d7444817c7eb6c1e4e8bf5208c2823c (diff) | |
download | sciteco-b44a4bae7d5c1098709dfaffd122263e0f4c5bc6.tar.gz |
standard lib: added getopt.tes for parsing command line options in scripts
* this uses an optstring compatible with getopt(3).
* It does not use repeated getopt calls to iterate options, though
but places the results in registers beginning with "getopt.".
E.g. option "C" will result in "getopt.C" being set after the
call to setopt.
String arguments are supported and are placed in the string part
of the getopt registers.
* The grosciteco.tes and symbols-extract.tes scripts make use of
getopt now, to simplify and clean up their command line handling.
Diffstat (limited to 'lib/getopt.tes')
-rw-r--r-- | lib/getopt.tes | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/getopt.tes b/lib/getopt.tes new file mode 100644 index 0000000..a2bcb31 --- /dev/null +++ b/lib/getopt.tes @@ -0,0 +1,52 @@ +!*$ + * M[getopt] -> Success|Failure -- Parse options + * + * Parses command line options according to the string + * \(lqoptstring\(rq, similar to \fBgetopt\P(3). + * \(lqoptstring\(rq is a list of single character + * option names. + * If followed by a colon, the option takes an argument + * (which can be part of the same command line argument as in + * \(lq-Ofoo\(rq). + * If followed by two colons, the option argument is optional. + * For every detected option \fIX\fP, a register \fBgetopt.\fIX\fR is + * defined with -1 (true) in its integer part. + * If the option had an argument, it is stored in the register's + * string part. + * + * A condition boolean is returned to signify whether + * there was a parsing error. + *! +[optstring] +@[getopt]{ + .U.d + < + .-Z"= 1; ' 0A-^^-"N :L; F< ' + + 1A-^^-"= K 1; ' + + 0U.i < + :Q[optstring]-Q.i"= Q.dJ 0 ' + Q.iQ[optstring]U.c + + 0U.#ar < + %.i-:Q[optstring]"= 1; ' + Q.iQ[optstring]-^^:"N 1; ' + %.#ar> + + 1A-Q.c"= + -U[getopt.U.c] + Q.#ar"> + 2A-10"= + K Q.#ar-1"> 0A-^^-"= 0U.i F< ' ' + | + 2D + ' + LR 0X[getopt.U.c] 0L + ' + K 1; + ' + > + > + Q.dJ +-1} |