From b44a4bae7d5c1098709dfaffd122263e0f4c5bc6 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 16 Nov 2016 16:28:03 +0100 Subject: 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. --- lib/Makefile.am | 2 +- lib/getopt.tes | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/getopt.tes (limited to 'lib') diff --git a/lib/Makefile.am b/lib/Makefile.am index a66aaef..69a7c02 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,6 +1,6 @@ dist_scitecolib_DATA = color.tes lexer.tes session.tes fnkeys.tes -dist_scitecolib_DATA += string.tes +dist_scitecolib_DATA += string.tes getopt.tes # Helper script for creating lexer definitions EXTRA_DIST = scite2co.lua 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} -- cgit v1.2.3