changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 655: 65102f74d1ae
parent: 3dd1924ad5ea
child: c5fe76568de0
author: Richard Westhaver <ellis@rwest.io>
date: Mon, 16 Sep 2024 21:28:33 -0400
permissions: -rw-r--r--
description: some optimizations, may have muddied the waters with cli-opt a bit though.. tbd
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 opts :type (vector cli-opt))
17  (cmds :initarg :cmds :initform (make-array 0 :element-type 'cli-cmd :adjustable t)
18  :accessor 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 (opts self))
39  (length (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 (cmds place)))
58 
59 (defmethod push-opt ((self cli-opt) (place cli-cmd))
60  (vector-push self (opts place)))
61 
62 (defmethod pop-cmd ((self cli-cmd))
63  (vector-pop (cmds self)))
64 
65 (defmethod pop-opt ((self cli-opt))
66  (vector-pop (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 (cmds self) :key #'cli-name :test #'string=)))
97  (if active
98  ;; maybe issue warning here? report to user
99  (when (cli-lock-p c)
100  c)
101  c)))
102 
103 (defmethod (setf find-cmd) ((new cli-cmd) (self cli-cmd) name &optional active)
104  (let ((match (find-cmd self name active) ))
105  (substitute new match (cmds self) :test 'cli-equal)))
106 
107 (defmethod active-cmds ((self cli-cmd))
108  (remove-if-not #'cli-lock-p (cmds self)))
109 
110 (defmethod activate-cmd ((self cli-cmd))
111  (setf (cli-lock-p self) t))
112 
113 (defmethod find-opts ((self cli-cmd) name &key active recurse)
114  (let ((ret))
115  (flet ((%find (o obj)
116  (when-let ((found (find o (opts obj) :key #'cli-opt-name :test 'equal)))
117  (push found ret))))
118  (when (and recurse (cmds self))
119  (loop for c across (cmds self)
120  do (%find name c)))
121  (%find name self)
122  (when active
123  (setf ret (remove-if-not #'cli-lock-p ret)))
124  ret)))
125 
126 (defmethod find-opt ((self cli-cmd) name &optional active)
127  (let ((ret (find name (opts self) :key #'cli-opt-name :test 'equal)))
128  (if active
129  (when (cli-opt-lock ret) ret)
130  ret)))
131 
132 (defmethod (setf find-opt) ((new cli-opt) (self cli-cmd) name &optional active)
133  (let ((match (find-opt self name active)))
134  (substitute new match (opts self) :test 'cli-equal)))
135 
136 (defmethod active-opts ((self cli-cmd) &optional global)
137  (remove-if-not
138  (if global
139  #'active-global-opt-p
140  #'cli-opt-lock)
141  (opts self)))
142 
143 (defmethod find-short-opts ((self cli-cmd) ch &key recurse)
144  (let ((ret))
145  (flet ((%find (ch obj)
146  (when-let ((found (find ch (opts obj) :key #'cli-opt-name :test #'opt-string-prefix-eq)))
147  (push found ret))))
148  (when (and recurse (cmds self))
149  (loop for c across (cmds self)
150  do (%find ch c)))
151  (%find ch self)
152  ret)))
153 
154 (declaim (inline solop))
155 (defun solop (self)
156  (= 0 (length (active-cmds self)) (length (active-opts self))))
157 
158 (defmacro with-opt-restart-case (arg condition)
159  "Bind restarts 'use-as-arg' and 'discard-arg' for duration of BODY."
160  `(restart-case ,condition
161  (use-as-arg () () (make-cli-node 'arg ,arg))
162  (discard-arg () () (setf ,arg nil))))
163 
164 (defmethod proc-args ((self cli-cmd) args)
165  "Process ARGS into an ast. Each element of the ast is a node with a
166 :kind slot, indicating the type of node and a :form slot which stores
167 an object."
168  (make-cli-ast
169  (loop
170  with skip
171  with exit
172  for (a . args) on args
173  if skip
174  do (setq skip nil)
175  else if exit
176  do (return)
177  ;; TODO 2024-09-15: handle flag groups -abcd
178  else if (short-opt-p a) ;; SHORT OPT
179  collect
180  (if-let ((o (car (find-short-opts self (aref a 1) :recurse nil))))
181  (%compose-short-opt o)
182  (with-opt-restart-case a
183  (clap-unknown-argument a 'cli-opt)))
184  else if (long-opt-p a) ;; LONG OPT
185  collect
186  (let* ((has-eq (long-opt-has-eq-p a))
187  (name (or (car has-eq) (string-left-trim "-" a)))
188  (o (car (find-opts self name :recurse nil))))
189  (cond
190  ((and has-eq o)
191  (setf (cli-opt-val o) (cdr has-eq))
192  (make-cli-node 'opt o))
193  ((and (not has-eq) o)
194  (prog1
195  (%compose-long-opt o (pop args))
196  (setq skip t)))
197  (t ;; (not o) (not has-eq)
198  (with-opt-restart-case a
199  (clap-unknown-argument a 'cli-opt)))))
200  ;; OPT GROUP
201  else if (opt-group-p a)
202  collect
203  (make-cli-node 'group nil)
204  ;; OPT KEYWORD (experimental)
205  else if (opt-keyword-p a)
206  collect (if-let ((o (car (find-opts self (string-left-trim ":" a) :recurse nil))))
207  (prog1 (%compose-keyword-opt o (pop args))
208  (setq exit t))
209  (make-cli-node 'arg a))
210  else ;; CMD or ARG
211  collect
212  (if-let ((cmd (find-cmd self a)))
213  (prog1 (make-cli-node 'cmd (parse-args cmd args :compile t))
214  (setq exit t))
215  ;; just a plain arg - move to next
216  (make-cli-node 'arg a)))))
217 
218 (defmethod install-ast ((self cli-cmd) (ast cli-ast))
219  "Install the given AST, recursively filling in value slots."
220  (with-slots (cmds opts) self
221  ;; we assume all nodes in the ast have been validated and the ast
222  ;; itself is consumed. validation is performed in proc-args.
223 
224  ;; before doing anything else we lock SELF, which should remain
225  ;; locked until all subcommands have completed
226  (activate-cmd self)
227  (loop named install
228  for (node . tail) on (ast ast)
229  while node
230  do
231  (let ((kind (cli-node-kind node))
232  (form (cli-node-form node)))
233  (case kind
234  ;; opts
235  (opt
236  (setf #1=(find-opt self (cli-name form)) form)
237  (activate-opt #1#)
238  (log:trace! (format nil "installing opt ~A" (cli-name form))))
239  (cmd
240  (setf (find-cmd self (cli-name form)) form)
241  (log:trace! (format nil "installing cmd ~A" (cli-name form))))
242  (arg (push-arg form self)))))
243  (setf (cli-cmd-args self) (nreverse (cli-cmd-args self)))
244  self))
245 
246 (defmethod install-thunk ((self cli-cmd) (lambda function) &optional compile)
247  "Install THUNK into the corresponding slot in cli-cmd SELF."
248  (let ((%thunk (if compile (compile nil lambda) lambda)))
249  (setf (cli-thunk self) %thunk)
250  self))
251 
252 (defmethod push-arg (arg (self cli-cmd))
253  "Push an ARG onto the corresponding slot of a CLI-CMD."
254  (push arg (cli-cmd-args self)))
255 
256 (defmethod parse-args ((self cli-cmd) args &key (compile t))
257  "Parse ARGS and return the updated object SELF.
258 ARGS is assumed to be a valid cli-ast (list of cli-nodes), unless COMPILE is
259 t, in which case a list of strings is assumed."
260  (with-slots (opts cmds) self
261  (let ((args (if compile (proc-args self args) args)))
262  (install-ast self args))))
263 
264 ;; WARNING: make sure to fill in the opt and cmd slots with values
265 ;; from the top-level args before calling a command.
266 (defmethod call-cmd ((self cli-cmd) args opts)
267  (trace! "calling command:" args opts)
268  (funcall (cli-thunk self) args opts))
269 
270 (defmethod do-opts ((self cli-cmd) &optional global)
271  (do-opts (active-opts self) global))
272 
273 (defmethod do-cmd ((self cli-cmd))
274  "Perform the active command or subcommand, recursively calling DO-CMD on
275 subcommands until a level is reached which satisfies SOLOP. active OPTS are
276 evaluated with DO-OPTS along the way."
277  (do-opts self)
278  (if (solop self)
279  (call-cmd self (cli-cmd-args self) (active-opts self))
280  (loop for c across (active-cmds self)
281  do (do-cmd c))))
282