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 3 ;; Command Objects used to build CLI Applications. 10 (in-package :cli/clap/obj) 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.")) 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))) 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" 40 (length (cli-cmd-args self))))) 42 (defmethod print-usage ((self cli-cmd) &optional stream) 43 (with-slots (opts cmds) self 44 (format stream "~(~A~) ~A~A~A" 46 (if-let ((d (and (slot-boundp self 'description) (cli-description self)))) 51 (format nil "~{~% ~A~^~}" (loop for o across opts collect (print-usage o nil)))) 54 (format nil "~{!~A~}" (loop for c across cmds collect (print-usage c nil))))))) 56 (defmethod push-cmd ((self cli-cmd) (place cli-cmd)) 57 (vector-push self (cmds place))) 59 (defmethod push-opt ((self cli-opt) (place cli-cmd)) 60 (vector-push self (opts place))) 62 (defmethod pop-cmd ((self cli-cmd)) 63 (vector-pop (cmds self))) 65 (defmethod pop-opt ((self cli-opt)) 66 (vector-pop (opts self))) 68 (defmethod handle-unknown-opt ((self cli-cmd) (opt string)) 69 (with-opt-restart-case opt 70 (clap-unknown-argument opt 'cli-opt))) 72 (defmethod handle-invalid-opt ((self cli-cmd) (opt string) &optional reason) 73 (clap-invalid-argument opt :kind 'cli-opt :reason reason)) 75 (defmethod handle-missing-opt ((self cli-cmd) (opt string)) 76 (clap-missing-argument opt 'cli-opt)) 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)) 84 (unless (member nil (loop for oa across opts 86 collect (cli-equal oa ob))) 88 (if (and (null cmds) (null bc)) 90 (unless (member nil (loop for ca across cmds 92 collect (cli-equal ca cb))) 95 (defmethod find-cmd ((self cli-cmd) name &optional active) 96 (when-let ((c (find name (cmds self) :key #'cli-name :test #'string=))) 98 ;; maybe issue warning here? report to user 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))) 107 (defmethod active-cmds ((self cli-cmd)) 108 (remove-if-not #'cli-lock-p (cmds self))) 110 (defmethod activate-cmd ((self cli-cmd)) 111 (setf (cli-lock-p self) t)) 113 (defmethod find-opts ((self cli-cmd) name &key active recurse) 115 (flet ((%find (o obj) 116 (when-let ((found (find o (opts obj) :key #'cli-opt-name :test 'equal))) 118 (when (and recurse (cmds self)) 119 (loop for c across (cmds self) 123 (setf ret (remove-if-not #'cli-lock-p ret))) 126 (defmethod find-opt ((self cli-cmd) name &optional active) 127 (let ((ret (find name (opts self) :key #'cli-opt-name :test 'equal))) 129 (when (cli-opt-lock ret) ret) 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))) 136 (defmethod active-opts ((self cli-cmd) &optional global) 139 #'active-global-opt-p 143 (defmethod find-short-opts ((self cli-cmd) ch &key recurse) 145 (flet ((%find (ch obj) 146 (when-let ((found (find ch (opts obj) :key #'cli-opt-name :test #'opt-string-prefix-eq))) 148 (when (and recurse (cmds self)) 149 (loop for c across (cmds self) 154 (declaim (inline solop)) 156 (= 0 (length (active-cmds self)) (length (active-opts self)))) 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)))) 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 172 for (a . args) on args 177 ;; TODO 2024-09-15: handle flag groups -abcd 178 else if (short-opt-p a) ;; SHORT OPT 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 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)))) 191 (setf (cli-opt-val o) (cdr has-eq)) 192 (make-cli-node 'opt o)) 193 ((and (not has-eq) o) 195 (%compose-long-opt o (pop args)) 197 (t ;; (not o) (not has-eq) 198 (with-opt-restart-case a 199 (clap-unknown-argument a 'cli-opt))))) 201 else if (opt-group-p a) 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)) 209 (make-cli-node 'arg a)) 212 (if-let ((cmd (find-cmd self a))) 213 (prog1 (make-cli-node 'cmd (parse-args cmd args :compile t)) 215 ;; just a plain arg - move to next 216 (make-cli-node 'arg a))))) 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. 224 ;; before doing anything else we lock SELF, which should remain 225 ;; locked until all subcommands have completed 228 for (node . tail) on (ast ast) 231 (let ((kind (cli-node-kind node)) 232 (form (cli-node-form node))) 236 (setf #1=(find-opt self (cli-name form)) form) 238 (log:trace! (format nil "installing opt ~A" (cli-name form)))) 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))) 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) 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))) 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)))) 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)) 270 (defmethod do-opts ((self cli-cmd) &optional global) 271 (do-opts (active-opts self) global)) 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." 279 (call-cmd self (cli-cmd-args self) (active-opts self)) 280 (loop for c across (active-cmds self)