changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/cli/clap/cmd.lisp

changeset 646: 95fd920af398
parent: 3e6a17fb5712
child: 74e563ed4537
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 11 Sep 2024 18:08:29 -0400
permissions: -rw-r--r--
description: error handling methods for clap
1 ;;; cli/clap/cmd.lisp --- Clap Commands
2 
3 ;; Command Objects used to build CLI Applications.
4 
5 ;;; Commentary:
6 
7 ;;
8 
9 ;;; Code:
10 (in-package :cli/clap/obj)
11 
12 (defclass cli-cmd ()
13  ;; name slot is required and must be a string
14  ((name :initarg :name :initform (required-argument :name) :accessor cli-name :type string)
15  (opts :initarg :opts :initform (make-array 0 :element-type 'cli-opt :adjustable t)
16  :accessor cli-opts :type (vector cli-opt))
17  (cmds :initarg :cmds :initform (make-array 0 :element-type 'cli-cmd :adjustable t)
18  :accessor cli-cmds :type (vector cli-cmd))
19  (thunk :initform #'default-thunk :initarg :thunk :accessor cli-thunk :type function-lambda-expression)
20  (lock :initform nil :initarg :lock :accessor cli-lock-p :type boolean)
21  (description :initarg :description :accessor cli-description :type string)
22  (args :initform nil :initarg :args :accessor cli-cmd-args))
23  (:documentation "CLI command class inherited by both the 'main' command which is executed when
24 a CLI is called without arguments, and all subcommands."))
25 
26 (defmethod initialize-instance :after ((self cli-cmd) &key)
27  (with-slots (name thunk opts cmds) self
28  (unless (stringp name) (setf name (format nil "~(~A~)" name)))
29  (unless (vectorp cmds) (setf cmds (make-cmds cmds)))
30  (unless (vectorp opts) (setf opts (make-opts opts)))
31  (when (symbolp thunk) (setf thunk (symbol-function thunk)))
32  self))
33 
34 (defmethod print-object ((self cli-cmd) stream)
35  (print-unreadable-object (self stream :type t)
36  (format stream "~A :opts ~A :cmds ~A :args ~A"
37  (cli-name self)
38  (length (cli-opts self))
39  (length (cli-cmds self))
40  (length (cli-cmd-args self)))))
41 
42 (defmethod print-usage ((self cli-cmd) &optional stream)
43  (with-slots (opts cmds) self
44  (format stream "~(~A~) ~A~A~A"
45  (cli-name self)
46  (if-let ((d (and (slot-boundp self 'description) (cli-description self))))
47  (format nil ": ~A" d)
48  "")
49  (if (null opts)
50  ""
51  (format nil "~{~% ~A~^~}" (loop for o across opts collect (print-usage o nil))))
52  (if (null cmds)
53  ""
54  (format nil "~{!~A~}" (loop for c across cmds collect (print-usage c nil)))))))
55 
56 (defmethod push-cmd ((self cli-cmd) (place cli-cmd))
57  (vector-push self (cli-cmds place)))
58 
59 (defmethod push-opt ((self cli-opt) (place cli-cmd))
60  (vector-push self (cli-opts place)))
61 
62 (defmethod pop-cmd ((self cli-cmd))
63  (vector-pop (cli-cmds self)))
64 
65 (defmethod pop-opt ((self cli-opt))
66  (vector-pop (cli-opts self)))
67 
68 (defmethod handle-unknown-opt ((self cli-cmd) (opt string))
69  (with-opt-restart-case opt
70  (clap-unknown-argument opt 'cli-opt)))
71 
72 (defmethod handle-invalid-opt ((self cli-cmd) (opt string) &optional reason)
73  (clap-invalid-argument opt :kind 'cli-opt :reason reason))
74 
75 (defmethod handle-missing-opt ((self cli-cmd) (opt string))
76  (clap-missing-argument opt 'cli-opt))
77 
78 (defmethod cli-equal ((a cli-cmd) (b cli-cmd))
79  (with-slots (name opts cmds) a
80  (with-slots ((bn name) (bo opts) (bc cmds)) b
81  (and (string= name bn)
82  (if (and (null opts) (null bo))
83  t
84  (unless (member nil (loop for oa across opts
85  for ob across bo
86  collect (cli-equal oa ob)))
87  t))
88  (if (and (null cmds) (null bc))
89  t
90  (unless (member nil (loop for ca across cmds
91  for cb across bc
92  collect (cli-equal ca cb)))
93  t))))))
94 
95 (defmethod find-cmd ((self cli-cmd) name &optional active)
96  (when-let ((c (find name (cli-cmds self) :key #'cli-name :test #'string=)))
97  (if active
98  ;; maybe issue warning here? report to user
99  (if (cli-lock-p c)
100  c
101  (clap-simple-error "inactive (unlocked) cmd: ~A" c))
102  c)))
103 
104 (defmethod active-cmds ((self cli-cmd))
105  (remove-if-not #'cli-lock-p (cli-cmds self)))
106 
107 (defmethod find-opts ((self cli-cmd) name &key active recurse)
108  (let ((ret))
109  (flet ((%find (o obj)
110  (when-let ((found (find o (cli-opts obj) :key #'cli-opt-name :test 'equal)))
111  (push found ret))))
112  (when (and recurse (cli-cmds self))
113  (loop for c across (cli-cmds self)
114  do (%find name c)))
115  (%find name self)
116  (when active
117  (setf ret (remove-if-not #'cli-lock-p ret)))
118  ret)))
119 
120 (defmethod active-opts ((self cli-cmd) &optional global)
121  (remove-if-not
122  (if global
123  #'active-global-opt-p
124  #'cli-opt-lock)
125  (cli-opts self)))
126 
127 (defmethod find-short-opts ((self cli-cmd) ch &key recurse)
128  (let ((ret))
129  (flet ((%find (ch obj)
130  (when-let ((found (find ch (cli-opts obj) :key #'cli-opt-name :test #'opt-string-prefix-eq)))
131  (push found ret))))
132  (when (and recurse (cli-cmds self))
133  (loop for c across (cli-cmds self)
134  do (%find ch c)))
135  (%find ch self)
136  ret)))
137 
138 (declaim (inline solop))
139 (defun solop (self)
140  (and (= 0 (length (active-cmds self)) (length (active-opts self)))))
141 
142 (defmacro with-opt-restart-case (arg condition)
143  "Bind restarts 'use-as-arg' and 'discard-arg' for duration of BODY."
144  `(restart-case ,condition
145  (use-as-arg () () (make-cli-node 'arg ,arg))
146  (discard-arg () () nil)))
147 
148 (defmethod proc-args ((self cli-cmd) args)
149  "Process ARGS into an ast. Each element of the ast is a node with a
150 :kind slot, indicating the type of node and a :form slot which stores
151 a value.
152 
153 For now we parse group separators '--' and insert a nil into the tree,
154 this will likely change to generating a new branch in the ast as it
155 should be."
156  (make-cli-ast
157  (let ((holes)) ;; list of arg indexes which can be skipped since they're
158  ;; consumed by an opt
159  (loop
160  for i below (length args)
161  for (a . args) on args
162  if (member i holes)
163  do (continue) ;; skip args which have been consumed already
164  ;; else
165  ;; if (= (length a) 1)
166  ;; collect (make-cli-node 'arg a) ; always treat single-char as arg
167  else
168  if (short-opt-p a) ;; SHORT OPT
169  collect
170  (if-let ((o (find-short-opts self (aref a 1) :recurse t)))
171  (%compose-short-opt (car o) a)
172  ;; TODO 2024-09-11: signal error?
173  (with-opt-restart-case a
174  (clap-unknown-argument a)))
175  else
176  if (long-opt-p a) ;; LONG OPT
177  collect
178  (let ((o (find-opts self (string-left-trim "-" a) :recurse t))
179  (has-eq (long-opt-has-eq-p a)))
180  (cond
181  ((and has-eq o)
182  (setf (cli-opt-val o) (cdr has-eq))
183  (make-cli-node 'opt o))
184  ((and (not has-eq) o)
185  (prog1 (%compose-long-opt (car o) args)
186  (push (1+ i) holes)))
187  (t ;; (not o) (not has-eq)
188  (with-opt-restart-case a
189  (clap-unknown-argument a)))))
190  ;; OPT GROUP
191  else
192  if (opt-group-p a)
193  collect nil
194  ;; CMD
195  else
196  collect
197  (let ((cmd (find-cmd self a)))
198  (if cmd
199  ;; TBD
200  (make-cli-node 'cmd (find-cmd self a))
201  ;; ARG
202  (make-cli-node 'arg a)))))))
203 
204 (defmethod install-ast ((self cli-cmd) (ast cli-ast))
205  "Install the given AST, recursively filling in value slots."
206  (with-slots (cmds opts) self
207  ;; we assume all nodes in the ast have been validated and the ast
208  ;; itself is consumed. validation is performed in proc-args.
209 
210  ;; before doing anything else we lock SELF, which should remain
211  ;; locked for the full runtime duration.
212  (setf (cli-lock-p self) t)
213  (loop named install
214  for (node . tail) on (debug! (ast ast))
215  until (null node)
216  do
217  (let ((kind (cli-node-kind node)) (form (cli-node-form node)))
218  (case kind
219  ;; opts
220  (opt
221  (let ((name (cli-opt-name form)))
222  (when-let ((o (car (find-opts self name))))
223  (setf o form)
224  (setf (cli-opt-lock o) t))))
225  ;; when we encounter a command we recurse over the tail
226  (cmd
227  (when-let ((c (find-cmd self (cli-name form))))
228  ;; handle the rest of the AST
229  (setf c (install-ast c (make-cli-ast tail)))
230  (return-from install)))
231  (arg (push-arg form self)))))
232  (setf (cli-cmd-args self) (nreverse (cli-cmd-args self)))
233  self))
234 
235 (defmethod install-thunk ((self cli-cmd) (lambda function) &optional compile)
236  "Install THUNK into the corresponding slot in cli-cmd SELF."
237  (let ((%thunk (if compile (compile nil lambda) lambda)))
238  (setf (cli-thunk self) %thunk)
239  self))
240 
241 (defmethod push-arg (arg (self cli-cmd))
242  "Push an ARG onto the corresponding slot of a CLI-CMD."
243  (push arg (cli-cmd-args self)))
244 
245 (defmethod parse-args ((self cli-cmd) args &key (compile t))
246  "Parse ARGS and return the updated object SELF.
247 ARGS is assumed to be a valid cli-ast (list of cli-nodes), unless COMPILE is
248 t, in which case a list of strings is assumed."
249  (with-slots (opts cmds) self
250  (let ((args (if compile (proc-args self args) args)))
251  (install-ast self args))))
252 
253 ;; WARNING: make sure to fill in the opt and cmd slots with values
254 ;; from the top-level args before calling a command.
255 (defmethod call-cmd ((self cli-cmd) args opts)
256  (trace! "calling command:" args opts)
257  (funcall (cli-thunk self) args opts))
258 
259 (defmethod do-cmd ((self cli-cmd))
260  "Perform the command, recursively calling child commands and opts if necessary."
261  (loop for o across (active-opts self)
262  do (do-opt o))
263  (if (solop self)
264  (call-cmd self (cli-cmd-args self) (active-opts self))
265  (loop for c across (active-cmds self)
266  do (do-cmd c))))
267