changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 643: f901de70a80e
parent: e49442cd6010
child: 6e5006dfe7b8
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 10 Sep 2024 21:26:30 -0400
permissions: -rw-r--r--
description: opt fixes and test updates
1 ;;; cli/clap/opt.lisp --- Clap Opts
2 
3 ;; CLI Opt Objects
4 
5 ;;; Code:
6 (in-package :cli/clap/obj)
7 
8 ;;; Parsers
9 (make-opt-parser string *arg*)
10 
11 (make-opt-parser boolean (when *arg* t))
12 
13 (make-opt-parser (form string) (read-from-string *arg*))
14 
15 (make-opt-parser (list form) (when (listp *arg*) *arg*))
16 
17 (make-opt-parser (symbol form) (when (symbolp *arg*) *arg*))
18 
19 (make-opt-parser (keyword form) (when (keywordp *arg*) *arg*))
20 
21 (make-opt-parser number (when *arg* (parse-number *arg*)))
22 
23 (make-opt-parser integer (when *arg* (parse-integer *arg*)))
24 
25 (make-opt-parser (file string)
26  (parse-native-namestring *arg* nil *default-pathname-defaults* :as-directory nil))
27 
28 (make-opt-parser (directory string)
29  (sb-ext:parse-native-namestring *arg* nil *default-pathname-defaults* :as-directory t))
30 
31 (make-opt-parser (pathname string)
32  (pathname *arg*))
33 
34 ;;; Objects
35 (defstruct cli-opt
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))
40  (val nil)
41  (global nil :type boolean)
42  (description nil :type (or null string))
43  (lock nil :type boolean))
44 
45 (defun %compose-short-opt (o arg)
46  (declare (ignorable arg))
47  (setf (cli-opt-val o) t)
48  (make-cli-node 'opt o))
49 
50 (defun %compose-long-opt (o args)
51  (declare (ignorable args))
52  (setf (cli-opt-val o) (or (pop args) t))
53  (make-cli-node 'opt o))
54 
55 (defmethod handle-unknown-argument ((self cli-opt) arg))
56 (defmethod handle-missing-argument ((self cli-opt) arg))
57 (defmethod handle-invalid-argument ((self cli-opt) arg))
58 
59 (defmethod initialize-instance :after ((self cli-opt) &key)
60  (with-slots (name thunk) self
61  (unless (stringp name) (setf name (format nil "~(~A~)" name)))
62  (when (symbolp thunk) (setf thunk (funcall (compile nil `(lambda () ,(symbol-function thunk))))))
63  self))
64 
65 (defmethod install-thunk ((self cli-opt) (lambda function) &optional compile)
66  "Install THUNK into the corresponding slot in cli-cmd SELF."
67  (let ((%thunk (if compile (compile nil lambda) lambda)))
68  (setf (cli-thunk self) %thunk)
69  self))
70 
71 (defmethod print-object ((self cli-opt) stream)
72  (print-unreadable-object (self stream :type t)
73  (format stream "~A :global ~A :val ~A"
74  (cli-opt-name self)
75  (cli-opt-global self)
76  (cli-opt-val self))))
77 
78 (defmethod print-usage ((self cli-opt) &optional stream)
79  (format stream "-~(~{~A~^/--~}~)~A~A"
80  (let ((n (cli-opt-name self)))
81  (declare (simple-string n))
82  (list (make-shorty n) n))
83  (if (cli-opt-global self) "* " " ")
84  (if-let ((d (and (slot-boundp self 'description) (cli-opt-description self))))
85  (format stream ": ~A" d)
86  "")))
87 
88 (defmethod cli-equal ((a cli-opt) (b cli-opt))
89  (with-slots (name global kind) a
90  (with-slots ((bn name) (bg global) (bk kind)) b
91  (and (equal name bn)
92  (eq global bg)
93  (equal kind bk)))))
94 
95 (defmethod call-opt ((self cli-opt) arg)
96  (when-let ((thunk (cli-opt-thunk self)))
97  (setf (cli-opt-val self) (funcall thunk arg))))
98 
99 (defmethod do-opt ((self cli-opt))
100  (call-opt self (cli-opt-val self)))
101 
102 (defmethod do-opts ((self vector) &optional global)
103  (declare (ignore global))
104  (loop for opt across self
105  do (do-opt opt)))
106 
107 (defun active-global-opt-p (opt)
108  "Return non-nil if OPT is active at runtime and global."
109  (and (cli-opt-lock opt) (cli-opt-global opt)))