Mercurial > core / lisp/lib/cli/clap/opt.lisp
changeset 654: |
3dd1924ad5ea |
parent: |
119532882cb1
|
child: |
65102f74d1ae |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Sun, 15 Sep 2024 22:23:16 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
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 nil :type (or null function symbol)) 41 (global nil :type boolean) 42 (description nil :type (or null string)) 43 (lock nil :type boolean)) 45 (defmethod activate-opt ((self cli-opt)) 46 (setf (cli-lock-p self) t)) 48 (defun %compose-short-opt (o) 49 (setf (cli-opt-val o) t) 50 (make-cli-node 'opt o)) 52 (defun %compose-long-opt (o &optional val) 53 (setf (cli-opt-val o) val) 54 (make-cli-node 'opt o)) 56 (defun %compose-keyword-opt (o val) 57 (setf (cli-opt-val o) val) 58 (make-cli-node 'opt o)) 60 (defmethod handle-unknown-argument ((self cli-opt) arg)) 61 (defmethod handle-missing-argument ((self cli-opt) arg)) 62 (defmethod handle-invalid-argument ((self cli-opt) arg)) 64 (defmethod initialize-instance :after ((self cli-opt) &key) 65 (with-slots (name thunk) self 66 (unless (stringp name) (setf name (format nil "~(~A~)" name))) 67 (when (symbolp thunk) (setf thunk (funcall (compile nil `(lambda () ,(symbol-function thunk)))))) 70 (defmethod install-thunk ((self cli-opt) (lambda function) &optional compile) 71 "Install THUNK into the corresponding slot in cli-cmd SELF." 72 (let ((%thunk (if compile (compile nil lambda) lambda))) 73 (setf (cli-thunk self) %thunk) 76 (defmethod print-object ((self cli-opt) stream) 77 (print-unreadable-object (self stream :type t) 78 (format stream "~A :global ~A :val ~A" 83 (defmethod print-usage ((self cli-opt) &optional stream) 84 (format stream "-~(~{~A~^/--~}~)~A~A" 85 (let ((n (cli-opt-name self))) 86 (declare (simple-string n)) 87 (list (make-shorty n) n)) 88 (if (cli-opt-global self) "* " " ") 89 (if-let ((d (and (slot-boundp self 'description) (cli-opt-description self)))) 90 (format stream ": ~A" d) 93 (defmethod cli-equal ((a cli-opt) (b cli-opt)) 94 (with-slots (name global kind) a 95 (with-slots ((bn name) (bg global) (bk kind)) b 100 (defmethod call-opt ((self cli-opt) arg) 101 (when-let ((thunk (cli-opt-thunk self))) 102 (setf (cli-opt-val self) (funcall thunk arg)))) 104 (defmethod do-opt ((self cli-opt)) 105 (call-opt self (cli-opt-val self))) 107 (defmethod do-opts ((self vector) &optional global) 108 (loop for opt across self 110 (when (cli-opt-global opt) 114 (defun active-global-opt-p (opt) 115 "Return non-nil if OPT is active at runtime and global." 116 (and (cli-opt-lock opt) (cli-opt-global opt)))