changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 698: 96958d3eb5b0
parent: 517c65b51e6b
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
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 'identity :type symbol)
40  (val nil)
41  (description nil :type (or null string))
42  (lock nil :type boolean))
43 
44 (defmethod cli-name ((self cli-opt))
45  (cli-opt-name self))
46 
47 (defmethod activate-opt ((self cli-opt))
48  (setf (cli-opt-lock self) t))
49 
50 (defmethod cli-lock-p ((self cli-opt))
51  (cli-opt-lock self))
52 
53 (defun %compose-short-opt (o)
54  (setf (cli-opt-val o) t)
55  (make-cli-node 'opt o))
56 
57 (defun %compose-long-opt (o &optional val)
58  (setf (cli-opt-val o) val)
59  (make-cli-node 'opt o))
60 
61 (defun %compose-keyword-opt (o val)
62  (setf (cli-opt-val o) val)
63  (make-cli-node 'opt o))
64 
65 (defmethod handle-unknown-argument ((self cli-opt) arg))
66 (defmethod handle-missing-argument ((self cli-opt) arg))
67 (defmethod handle-invalid-argument ((self cli-opt) arg))
68 
69 (defmethod initialize-instance :after ((self cli-opt) &key)
70  (with-slots (name thunk) self
71  (unless (stringp name) (setf name (format nil "~(~A~)" name)))
72  self))
73 
74 (defmethod make-load-form ((obj cli-opt) &optional env)
75  (make-load-form-saving-slots
76  obj
77  :slot-names '(name kind thunk val description lock)
78  :environment env))
79 
80 (defmethod install-thunk ((self cli-opt) (lambda function) &optional compile)
81  "Install THUNK into the corresponding slot in cli-cmd SELF."
82  (let ((%thunk (if compile (compile nil lambda) lambda)))
83  (setf (cli-thunk self) %thunk)
84  self))
85 
86 (defmethod print-object ((self cli-opt) stream)
87  (print-unreadable-object (self stream :type t)
88  (format stream "~A :active ~A :val ~A"
89  (cli-opt-name self)
90  (cli-opt-lock self)
91  (cli-opt-val self))))
92 
93 (defmethod print-usage ((self cli-opt) &optional stream)
94  (format stream "-~(~{~A~^/--~}~) ~A"
95  (let ((n (cli-opt-name self)))
96  (declare (simple-string n))
97  (list (make-shorty n) n))
98  (if-let ((d (and (slot-boundp self 'description) (cli-opt-description self))))
99  (format stream ": ~A" d)
100  "")))
101 
102 (defmethod cli-equal ((a cli-opt) (b cli-opt))
103  (with-slots (name kind) a
104  (with-slots ((bn name) (bk kind)) b
105  (and (equal name bn)
106  (equal kind bk)))))
107 
108 (defmethod call-opt ((self cli-opt) arg)
109  (funcall (cli-opt-thunk self) arg))
110 
111 (defmethod do-opt ((self cli-opt))
112  (setf (cli-opt-val self) (call-opt self (cli-opt-val self))))
113 
114 (defmethod do-opts ((self vector))
115  (loop for opt across self
116  do (do-opt opt)))