Coverage report: /home/ellis/comp/core/lib/cli/clap/opt.lisp
Kind | Covered | All | % |
expression | 78 | 253 | 30.8 |
branch | 5 | 26 | 19.2 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
;;; cli/clap/opt.lisp --- Clap Opts
6
(in-package :cli/clap/obj)
9
(make-opt-parser string *arg*)
11
(make-opt-parser boolean (when *arg* t))
13
(make-opt-parser (form string) (read-from-string *arg*))
15
(make-opt-parser (list form) (when (listp *arg*) *arg*))
17
(make-opt-parser (symbol form) (when (symbolp *arg*) *arg*))
19
(make-opt-parser (keyword form) (when (keywordp *arg*) *arg*))
21
(make-opt-parser number (when *arg* (parse-number *arg*)))
23
(make-opt-parser integer (when *arg* (parse-integer *arg*)))
25
(make-opt-parser (file string)
26
(parse-native-namestring *arg* nil *default-pathname-defaults* :as-directory nil))
28
(make-opt-parser (directory string)
29
(sb-ext:parse-native-namestring *arg* nil *default-pathname-defaults* :as-directory t))
31
(make-opt-parser (pathname string)
36
;; note that cli-opts can have a nil or unbound name slot
37
(name "" :type string)
38
(kind 'boolean :type (or symbol list))
39
(thunk 'default-opt-thunk :type symbol)
41
(description nil :type (or null string))
42
(lock nil :type boolean))
44
(defaccessor cli-thunk ((self cli-opt)) (cli-opt-thunk self))
45
(defaccessor cli-name ((self cli-opt)) (cli-opt-name self))
47
(defmethod activate-opt ((self cli-opt))
48
(setf (cli-opt-lock self) t))
50
(defmethod cli-lock-p ((self cli-opt))
53
(defun %compose-flag-opt (o)
55
(setf (cli-opt-val o) t)
56
(make-cli-node 'opt o))
58
(defun %compose-flag-opts (&rest os)
61
(%compose-flag-opt o))))
63
(defun %compose-value-opt (o &optional val)
65
(setf (cli-opt-val o) val)
66
(make-cli-node 'opt o))
68
(defun %compose-keyword-opt (o val)
70
(setf (cli-opt-val o) val)
71
(make-cli-node 'opt o))
73
(defmethod initialize-instance :after ((self cli-opt) &key)
74
(with-slots (name thunk) self
75
(unless (stringp name) (setf name (format nil "~(~A~)" name)))
78
(defmethod make-load-form ((obj cli-opt) &optional env)
79
(make-load-form-saving-slots
81
:slot-names '(name kind thunk val description lock)
84
(defmethod install-thunk ((self cli-opt) (lambda function) &optional compile)
85
"Install THUNK into the corresponding slot in cli-cmd SELF."
86
(let ((%thunk (if compile (compile nil lambda) lambda)))
87
(setf (cli-thunk self) %thunk)
90
(defmethod print-object ((self cli-opt) stream)
91
(print-unreadable-object (self stream :type t)
92
(format stream "~A :active ~A :val ~A"
97
(defmethod print-usage ((self cli-opt) &optional stream)
98
(format stream "-~(~{~A~^/--~}~)~@[ :value ~A~]~24t~@[~A~]~@[~%~4t:doc ~A~]"
99
(let ((n (cli-opt-name self)))
100
(declare (simple-string n))
101
(list (make-shorty n) n))
102
(and (slot-boundp self 'val) (cli-opt-val self))
103
(and (slot-boundp self 'description) (cli-opt-description self))
104
(when (fboundp (cli-thunk self))
105
(documentation (symbol-function (cli-thunk self)) 'function))))
107
(defmethod equiv ((a cli-opt) (b cli-opt))
108
(with-slots (name kind) a
109
(with-slots ((bn name) (bk kind)) b
113
(defmethod equiv ((a t) (b cli-opt))
114
(equalp (cli-opt-val b) a))
116
(defmethod equiv ((a cli-opt) (b t))
117
(equalp (cli-opt-val a) b))
119
(defmethod call-opt ((self cli-opt) arg)
120
(funcall (cli-opt-thunk self) arg))
122
(defmethod do-opt ((self cli-opt))
123
(prog1 (setf (cli-opt-val self) (call-opt self (cli-opt-val self)))
124
(setf (cli-opt-lock self) nil)))
126
(defmethod do-opts ((self vector))
127
(loop for opt across self
131
(((name string) (self list) &key active default)
132
(if-let ((found (find name self :key 'cli-opt-name :test 'equal)))
134
(when (cli-lock-p found)
138
(((name string) (self vector) &key active default)
139
(if-let ((found (find name self :key 'cli-opt-name :test 'equal)))
141
(when (cli-lock-p found)
146
(defun getopt (name &optional (default :error) (opts *opts*))
147
"Retrieve a CLI-OPT-VAL by name from a vector of CLI-OPTs."
148
(let ((opts (or opts (opts *cli*))))
149
(cli-opt-val (find-opt
150
(string-downcase name) opts
151
:default (if (eql default :error)
152
(clap-unknown-argument name 'opt)
155
(defun setopt (name val &optional (default :error) (opts *opts*))
156
(let ((opts (or opts (opts *cli*))))
159
(string-downcase name) opts
160
:default (if (eql default :error)
161
(clap-unknown-argument name 'opt)
165
(defsetf getopt setopt)
167
(defmacro with-opt-restart-case (arg expression)
168
"Bind restarts 'use-as-arg' and 'discard-arg' for duration of EXPRESSION."
169
`(restart-case ,expression
170
(use-as-arg () () (make-cli-node 'arg ,arg))
171
(discard-arg () () (setf ,arg nil))))