Mercurial > core / lisp/lib/cli/clap/cmd.lisp
changeset 649: |
6e5006dfe7b8 |
parent: |
74e563ed4537
|
child: |
328e1ff73938 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Thu, 12 Sep 2024 22:38:22 -0400 |
permissions: |
-rw-r--r-- |
description: |
clap parsing updates |
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 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.")) 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" 38 (length (cli-opts self)) 39 (length (cli-cmds self)) 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 (cli-cmds place))) 59 (defmethod push-opt ((self cli-opt) (place cli-cmd)) 60 (vector-push self (cli-opts place))) 62 (defmethod pop-cmd ((self cli-cmd)) 63 (vector-pop (cli-cmds self))) 65 (defmethod pop-opt ((self cli-opt)) 66 (vector-pop (cli-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 (cli-cmds self) :key #'cli-name :test #'string=))) 98 ;; maybe issue warning here? report to user 103 (defmethod active-cmds ((self cli-cmd)) 104 (remove-if-not #'cli-lock-p (cli-cmds self))) 106 (defmethod activate-cmd ((self cli-cmd)) 107 (setf (cli-lock-p self) t)) 109 (defmethod find-opts ((self cli-cmd) name &key active recurse) 111 (flet ((%find (o obj) 112 (when-let ((found (find o (cli-opts obj) :key #'cli-opt-name :test 'equal))) 114 (when (and recurse (cli-cmds self)) 115 (loop for c across (cli-cmds self) 119 (setf ret (remove-if-not #'cli-lock-p ret))) 122 (defmethod active-opts ((self cli-cmd) &optional global) 125 #'active-global-opt-p 129 (defmethod find-short-opts ((self cli-cmd) ch &key recurse) 131 (flet ((%find (ch obj) 132 (when-let ((found (find ch (cli-opts obj) :key #'cli-opt-name :test #'opt-string-prefix-eq))) 134 (when (and recurse (cli-cmds self)) 135 (loop for c across (cli-cmds self) 140 (declaim (inline solop)) 142 (and (= 0 (length (active-cmds self)) (length (active-opts self))))) 144 (defmacro with-opt-restart-case (arg condition) 145 "Bind restarts 'use-as-arg' and 'discard-arg' for duration of BODY." 146 `(restart-case ,condition 147 (use-as-arg () () (make-cli-node 'arg ,arg)) 148 (discard-arg () () (setf ,arg nil)))) 150 (defmethod proc-args ((self cli-cmd) args) 151 "Process ARGS into an ast. Each element of the ast is a node with a 152 :kind slot, indicating the type of node and a :form slot which stores 157 for i below (length args) 158 for (a . args) on args 161 else if (short-opt-p a) ;; SHORT OPT 163 (if-let ((o (car (find-short-opts self (aref a 1) :recurse t)))) 164 (%compose-short-opt o) 165 ;; TODO 2024-09-11: signal error? 166 (with-opt-restart-case a 167 (clap-unknown-argument a))) 168 else if (long-opt-p a) ;; LONG OPT 170 (let ((o (car (find-opts self (string-left-trim "-" a) :recurse t))) 171 (has-eq (long-opt-has-eq-p a))) 174 (setf (cli-opt-val o) (cdr has-eq)) 175 (make-cli-node 'opt o)) 176 ((and (not has-eq) o) 178 (%compose-long-opt o (pop args)) 180 (t ;; (not o) (not has-eq) 181 (with-opt-restart-case a 182 (clap-unknown-argument a))))) 184 else if (opt-group-p a) 185 collect (make-cli-node 'group nil) 188 (let ((cmd (find-cmd self a))) 191 (make-cli-node 'cmd cmd) 193 (make-cli-node 'arg a)))))) 195 (defmethod install-ast ((self cli-cmd) (ast cli-ast)) 196 "Install the given AST, recursively filling in value slots." 197 (with-slots (cmds opts) self 198 ;; we assume all nodes in the ast have been validated and the ast 199 ;; itself is consumed. validation is performed in proc-args. 201 ;; before doing anything else we lock SELF, which should remain 202 ;; locked until all subcommands have completed 205 for (node . tail) on (ast ast) 208 (let ((kind (cli-node-kind node)) (form (cli-node-form node))) 212 (let ((name (cli-opt-name form))) 213 (when-let ((o (car (find-opts self name)))) 215 (setf (cli-opt-lock o) t)))) 216 ;; when we encounter a command we recurse over the tail 218 (when-let ((c (find-cmd self (cli-name form)))) 219 ;; handle the rest of the AST 220 (setf c (install-ast c (make-cli-ast tail))) 221 (return-from install))) 222 (arg (push-arg form self))))) 223 (setf (cli-cmd-args self) (nreverse (cli-cmd-args self))) 226 (defmethod install-thunk ((self cli-cmd) (lambda function) &optional compile) 227 "Install THUNK into the corresponding slot in cli-cmd SELF." 228 (let ((%thunk (if compile (compile nil lambda) lambda))) 229 (setf (cli-thunk self) %thunk) 232 (defmethod push-arg (arg (self cli-cmd)) 233 "Push an ARG onto the corresponding slot of a CLI-CMD." 234 (push arg (cli-cmd-args self))) 236 (defmethod parse-args ((self cli-cmd) args &key (compile t)) 237 "Parse ARGS and return the updated object SELF. 238 ARGS is assumed to be a valid cli-ast (list of cli-nodes), unless COMPILE is 239 t, in which case a list of strings is assumed." 240 (with-slots (opts cmds) self 241 (let ((args (if compile (proc-args self args) args))) 242 (install-ast self args)))) 244 ;; WARNING: make sure to fill in the opt and cmd slots with values 245 ;; from the top-level args before calling a command. 246 (defmethod call-cmd ((self cli-cmd) args opts) 247 (trace! "calling command:" args opts) 248 (funcall (cli-thunk self) args opts)) 250 (defmethod do-cmd ((self cli-cmd)) 251 "Perform the command, recursively calling child commands and opts if necessary." 252 (loop for o across (active-opts self) 255 (call-cmd self (cli-cmd-args self) (active-opts self)) 256 (loop for c across (active-cmds self)