summaryrefslogtreecommitdiffstats
path: root/share
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-12-12 15:17:15 -0800
committerKaz Kylheku <kaz@kylheku.com>2019-12-13 08:18:46 -0800
commit2eaa8bdbb3a6d426a9de4d916dad63f77fd54092 (patch)
tree78afe7ac5261539fa266f36bb36c84538f8518b6 /share
parentfdba58530a48223ecd0c9bcf629f08c3569d6c75 (diff)
downloadtxr-2eaa8bdbb3a6d426a9de4d916dad63f77fd54092.tar.gz
txr-2eaa8bdbb3a6d426a9de4d916dad63f77fd54092.tar.bz2
txr-2eaa8bdbb3a6d426a9de4d916dad63f77fd54092.zip
define-option-struct: use multiple inheritance
* share/txr/stdlib/getopts.tl (sys:option-base): New struct type, holding the boiler-plate methods and slots that were generated into the user-defined struct by define-option-struct. (define-option-struct): Inject the required properties by inheritance from sys:option-base, greatly reducing code bloat in the macro. The slot hash and opt-desc-list static slots have to be redefined in the derived structure so that type has its own instance of them.
Diffstat (limited to 'share')
-rw-r--r--share/txr/stdlib/getopts.tl36
1 files changed, 22 insertions, 14 deletions
diff --git a/share/txr/stdlib/getopts.tl b/share/txr/stdlib/getopts.tl
index a556493d..be6dec4f 100644
--- a/share/txr/stdlib/getopts.tl
+++ b/share/txr/stdlib/getopts.tl
@@ -341,25 +341,33 @@
(put-line ln)))
(put-line))))
-(defmacro define-option-struct (name super . opts)
+
+(defstruct sys:option-base nil
+ in-args
+ out-args
+ (:static slot-hash)
+ (:static opt-desc-list)
+ (:method add-opt (me opt)
+ (slotset me [me.slot-hash (or opt.desc.long opt.desc.short)] opt.arg))
+ (:method getopts (me args)
+ (set me.in-args args me.out-args args)
+ (let ((opr (new sys:opt-processor od-list me.opt-desc-list opts me)))
+ opr.(parse-opts args)))
+ (:method opthelp (me : (stream *stdout*))
+ (opthelp me.opt-desc-list stream)))
+
+(defmacro define-option-struct (name super-spec . opts)
(let* ((slots (mapcar (tb ((short long . rest))
(or long short))
- opts)))
- ^(defstruct ,name ,super
+ opts))
+ (supers (if (and super-spec (atom super-spec))
+ (list super-spec)
+ super-spec)))
+ ^(defstruct ,name (,*supers sys:option-base)
,*slots
- in-args
- out-args
(:static slot-hash #H(() ,*(mapcar [juxt symbol-name identity] slots)))
(:static opt-desc-list ',(mapcar (tb ((short long . rest))
(opt (if short (symbol-name short))
(if long (symbol-name long))
. rest))
- opts))
- (:method add-opt (me opt)
- (slotset me [me.slot-hash (or opt.desc.long opt.desc.short)] opt.arg))
- (:method getopts (me args)
- (set me.in-args args me.out-args args)
- (let ((opr (new sys:opt-processor od-list me.opt-desc-list opts me)))
- opr.(parse-opts args)))
- (:method opthelp (me : (stream *stdout*))
- (opthelp me.opt-desc-list stream)))))
+ opts)))))