diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2019-11-28 17:16:00 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2019-11-28 17:16:00 -0800 |
commit | 14b727861ffad78a82ea00980eba24b5f90d7325 (patch) | |
tree | 615a1af6b7ed20b8377eec7d2124ae350ef833da /share | |
parent | bce263cf9df425b896b62e24589ccebf6e703a88 (diff) | |
download | txr-14b727861ffad78a82ea00980eba24b5f90d7325.tar.gz txr-14b727861ffad78a82ea00980eba24b5f90d7325.tar.bz2 txr-14b727861ffad78a82ea00980eba24b5f90d7325.zip |
getopts: move opts object into opt-processor.
The methods of sys:opt-processor pass down an argument called
out which is an opts instance that is created in parse-opts.
In this patch, we move that argument into a new slot of the
sys:opt-processor object, named opts. We instantiate the opts
structure in the getopts function instead. The motivation here
is that (meth sys:opt-processor parse-opts) doesn't know which
type of opts object it is working with; its user is now
responsible for instantiating the object and installing it as
the opts slot.
* share/txr/stdlib/getopts.tl (sys:opt-processor): New
slot, opts.
(sys:opt-processor (parse-long, parse-shorts): Drop out
argument; refer to me.opts instead of it.
(sys:opt-processor parse-opts): Don't instantiate opts object
here, and don't pass it to parse-long and parse-shorts which
no longer take that argument. Rather, rely on it already being
installed into the opts slot.
(getopts): Instantiate opts here, and specify it as the opts
slot of the sys:opt-processor object also being instantiated
here.
Diffstat (limited to 'share')
-rw-r--r-- | share/txr/stdlib/getopts.tl | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/share/txr/stdlib/getopts.tl b/share/txr/stdlib/getopts.tl index a7c27d94..f155b238 100644 --- a/share/txr/stdlib/getopts.tl +++ b/share/txr/stdlib/getopts.tl @@ -52,6 +52,7 @@ (defstruct sys:opt-processor nil od-list (od-hash (hash :equal-based)) ;; string to opt-desc + opts (:postinit (me) me.(build-hash))) @@ -163,49 +164,51 @@ (error "opt-processor: duplicate option ~s" str)) (set [me.od-hash str] od)))) -(defmeth sys:opt-processor parse-long (me out opt : arg) +(defmeth sys:opt-processor parse-long (me opt : arg) (iflet ((ieq (unless (stringp arg) (break-str opt "=")))) (let ((oname [opt 0..ieq]) (arg [opt (succ ieq)..:])) - me.(parse-long out oname arg)) - (let ((od [me.od-hash opt])) + me.(parse-long oname arg)) + (let ((od [me.od-hash opt]) + (opts me.opts)) (cond ((null od) (sys:opt-err "unrecognized option: --~a" opt)) ((and arg od.arg-p) - out.(add (new (sys:opt-parsed opt arg od)))) + opts.(add (new (sys:opt-parsed opt arg od)))) ((stringp arg) (sys:opt-err "option --~a doesn't take an argument" opt)) (od.arg-p - (iflet ((arg (pop out.out-args))) - out.(add (new (sys:opt-parsed opt arg od))) + (iflet ((arg (pop opts.out-args))) + opts.(add (new (sys:opt-parsed opt arg od))) (sys:opt-err "option --~a requires an argument" opt))) - (out.(add (new (sys:opt-parsed opt arg od)))))))) + (t opts.(add (new (sys:opt-parsed opt arg od)))))))) -(defmeth sys:opt-processor parse-shorts (me out opts) +(defmeth sys:opt-processor parse-shorts (me opts) (each ((o (split-str opts #//))) - (iflet ((od [me.od-hash o])) + (iflet ((opts me.opts) + (od [me.od-hash o])) (let ((arg (when od.arg-p (when (> (length opts) 1) (sys:opt-err "argument -~a includes -~a, which does not clump" opts o)) - (unless out.out-args + (unless opts.out-args (sys:opt-err "option -~a requires an argument" o)) - (pop out.out-args)))) - out.(add (new (sys:opt-parsed o arg od)))) + (pop opts.out-args)))) + opts.(add (new (sys:opt-parsed o arg od)))) (sys:opt-err "unrecognized option: -~a" o)))) (defmeth sys:opt-processor parse-opts (me args) - (let ((out (new opts in-args args out-args args))) - (whilet ((arg (pop out.out-args))) + (let ((opts me.opts)) + (whilet ((arg (pop opts.out-args))) (cond ((equal "--" arg) (return)) - ((r^ #/--no-/ arg) me.(parse-long out [arg 5..:] :explicit-no)) - ((r^ #/--/ arg) me.(parse-long out [arg 2..:])) - ((r^ #/-.+/ arg) me.(parse-shorts out [arg 1..:])) - (t (push arg out.out-args) + ((r^ #/--no-/ arg) me.(parse-long [arg 5..:] :explicit-no)) + ((r^ #/--/ arg) me.(parse-long [arg 2..:])) + ((r^ #/-.+/ arg) me.(parse-shorts [arg 1..:])) + (t (push arg opts.out-args) (return)))) - out)) + opts)) (defun sys:wdwrap (string columns) (let ((words (tok-str string #/\S+/)) @@ -234,7 +237,8 @@ (new opt-desc short short long long helptext helptext type type)) (defun getopts (opt-desc-list args) - (let ((opr (new sys:opt-processor od-list opt-desc-list))) + (let* ((opts (new opts in-args args out-args args)) + (opr (new sys:opt-processor od-list opt-desc-list opts opts))) opr.(parse-opts args))) (defun opthelp (opt-desc-list : (stream *stdout*)) |