changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 683: c5fe76568de0
parent: 5e8b1855f866
child: 517c65b51e6b
author: Richard Westhaver <ellis@rwest.io>
date: Sat, 28 Sep 2024 17:48:06 -0400
permissions: -rw-r--r--
description: fixed clap objects to support make-load-form method - thunk is symbol only
1 ;;; cli/clap/cli.lisp --- Clap CLI Class
2 
3 ;; Top-level command object of a CLI App
4 
5 ;;; Code:
6 (in-package :cli/clap/obj)
7 
8 (defun make-cli (kind &rest slots)
9  "Creates a new CLI object of the given kind."
10  (declare (type (member :opt :cmd :cli t) kind))
11  (cond
12  ((eql kind :cli) (apply #'make-instance 'cli slots))
13  ((eql kind :opt) (apply #'make-cli-opt slots))
14  ((eql kind :cmd) (apply #'make-instance 'cli-cmd slots))
15  (t (apply #'make-instance kind slots))))
16 
17 (defmacro define-cli (sym &key name version #+nil (help t) description thunk opts cmds)
18  "Define a symbol NAME bound to a top-level CLI object."
19  (with-gensyms (%name %class)
20  (if (atom sym)
21  (setq %name sym
22  %class :cli)
23  (setq %name (car sym)
24  %class (cdr sym)))
25  ;; (when help)
26  `(,*default-cli-def* ,%name (make-cli ,%class :name ,name
27  :version ,version
28  :description ,description
29  :thunk ',thunk
30  :opts ,(make-opts opts)
31  :cmds ,(make-cmds cmds)))))
32 
33 (defmacro defmain (name (&key (exit t)) &body body)
34  "Define a CLI main function in the current package."
35  `(let ((*no-exit* ,(not exit)))
36  (defun ,name ()
37  "Run the top-level function and print to *STDOUT*."
38  (with-cli-handlers
39  (progn
40  ,@body)))))
41 
42 ;; RESEARCH 2023-09-12: closed over hash-table with short/long flags
43 ;; to avoid conflicts. if not, need something like a flag-function
44 ;; slot at class allocation.
45 (defun make-opts (opts)
46  "Make a vector of CLI-OPTs based on OPTS."
47  (map 'vector
48  (lambda (x)
49  (etypecase x
50  (string (make-cli-opt :name x))
51  (list (apply #'make-cli :opt x))
52  (t (make-cli :opt :name (format nil "~(~A~)" x) :global t))))
53  opts))
54 
55 (defun make-cmds (cmds)
56  "Make a vector of CLI-CMDs based on CMDS."
57  (map 'vector
58  (lambda (x)
59  (etypecase x
60  (cli-cmd x)
61  (string (make-cli :cmd :name x))
62  (list (apply #'make-cli :cmd x))
63  (t (make-cli :cmd :name (format nil "~(~A~)" x)))))
64  cmds))
65 
66 (defclass cli (cli-cmd)
67  ;; name slot defaults to *package*, must be string
68  ((name :initarg :name :initform (string-downcase (package-name *package*)) :accessor cli-name :type string)
69  (version :initarg :version :initform "0.1.0" :accessor cli-version :type string)
70  ;; TODO 2023-10-11: look into pushd popd - cd-stack?
71  (cd :initarg :cd :initform (sb-posix:getcwd) :type string :accessor cli-cd
72  :documentation "working directory of the top-level CLI."))
73  (:documentation "CLI"))
74 
75 (defmethod print-usage ((self cli) &optional stream)
76  (iprintln (format nil "usage: ~A [global] <command> [<arg>]~%" (cli-name self)) 2 stream))
77 
78 (defmethod print-version ((self cli) &optional stream)
79  (println (cli-version self) stream))
80 
81 (defmethod print-help ((self cli) &optional (stream t))
82  (println (format nil "~A v~A --- ~A~%" (cli-name self) (cli-version self) (cli-description self)) stream)
83  (print-usage self stream)
84  ;; (terpri stream)
85  (println "options:" stream)
86  (with-slots (opts cmds) self
87  (unless (null opts)
88  (loop for o across opts
89  do (iprintln (print-usage o nil) 2 stream)))
90  (terpri stream)
91  (println "commands:" stream)
92  (unless (null cmds)
93  (loop for c across cmds
94  do (iprintln (print-usage c nil) 2 stream)))))
95 
96 (defmethod cli-equal :before ((a cli) (b cli))
97  "Return T if A is the same cli object as B.
98 
99 Currently this function is intended only for instances of the CLI
100 class and is used as a specialized EQL for DEFINE-CONSTANT."
101  (with-slots (version) a
102  (with-slots ((bv version)) b
103  (string= version bv))))
104 
105 (declaim (inline debug-opts))
106 (defun debug-opts (cli)
107  (let ((o (active-opts cli))
108  (a (cli-cmd-args cli))
109  (c (active-cmds cli)))
110  (log:debug! :pwd (cli-cd cli) :active-opts o :cmd-args a :active-cmds c)))
111 
112 (defmacro with-cli ((cli &rest slots) args &body body)
113  "Like with-slots with some extra bindings.
114 
115 SLOTS is a list passed to WITH-SLOTS.
116 
117 CLI is updated based on the current environment and dynamically bound
118 to *CLI*. ARGS is a list of CLI args, defaults to *POSIX-ARGV* at
119 runtime if nil."
120  `(progn
121  (let ((*cli* ,cli))
122  (setf (cli-cd *cli*) *default-pathname-defaults*)
123  (with-slots ,slots (parse-args *cli* ,args :compile t)
124  ,@body))))