refactor(cli): minor refactors & allow flychecker in CLIs

- Add comments to doom-cli-context-parse.
- Simplify/inline code here and there.
- Remove sacrifice to appease byte-compiler.

Thanks to a346928 there's no need to appease the byte-compiler with this
eval-when-compile hackery.

Ref: a34692826f18
This commit is contained in:
Henrik Lissner 2022-06-22 19:34:25 +02:00
parent e651547abb
commit 001065ba42
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 29 additions and 34 deletions

View file

@ -121,7 +121,9 @@ OPTIONS:
" +" " " (format "'%s'" formatted))) " +" " " (format "'%s'" formatted)))
" or "))))))))) " or ")))))))))
(defcli! (:root :version) ((simple? ("--simple")) &context context) (defcli! (:root :version)
((simple? ("--simple"))
&context context)
"Show installed versions of Doom, Doom modules, and Emacs." "Show installed versions of Doom, Doom modules, and Emacs."
(doom/version) (doom/version)
(unless simple? (unless simple?
@ -408,8 +410,7 @@ The alist's CAR are lists of formatted switches plus their arguments, e.g.
(insert! (insert!
("%s%s\n%s" ("%s%s\n%s"
(mapconcat (mapconcat
(fn! (fn! (when (member "..." (cdr %))
(when (member "..." (cdr %))
(setq multiple? t)) (setq multiple? t))
(string-trim-right (string-trim-right
(format "%s %s" (format "%s %s"

View file

@ -1,13 +1,7 @@
;;; core/core-cli-lib.el --- API+DSL for Doom's CLI framework -*- lexical-binding: t; no-byte-compile: t; -*- ;;; core/core-cli-lib.el --- API+DSL for Doom's CLI framework -*- lexical-binding: t; -*-
;;; Commentary: ;;; Commentary:
;;; Code: ;;; Code:
;; Appease byte-compiler-sama
(eval-when-compile
;; ...but prevent recursive or unwanted loads
(unless (or load-in-progress (not noninteractive))
(require 'core-cli)))
(require 'seq) (require 'seq)
(require 'map) (require 'map)
@ -267,8 +261,7 @@ If NOLOAD? is non-nil, don't autoload deferred CLIs (see `doom-cli-get')."
(when cli (when cli
(cons (cons
cli (let (alias paths) cli (let (alias paths)
(while (and (doom-cli-p cli) (while (setq alias (ignore-errors (doom-cli-alias cli)))
(setq alias (doom-cli-alias cli)))
(and (setq cli (doom-cli-get alias t noload?)) (and (setq cli (doom-cli-get alias t noload?))
(push cli paths))) (push cli paths)))
(nreverse paths))))) (nreverse paths)))))
@ -290,7 +283,7 @@ Returned in the order they will execute. Includes pseudo CLIs."
(dolist (path (nreverse paths)) (dolist (path (nreverse paths))
(push (cons :before path) results)) (push (cons :before path) results))
(push '(:before) results) (push '(:before) results)
(dolist (result (nreverse results) clis) (dolist (result results (nreverse clis))
(when-let (cli (doom-cli-get result t)) (when-let (cli (doom-cli-get result t))
(cl-pushnew cli clis (cl-pushnew cli clis
:test #'equal :test #'equal
@ -374,7 +367,7 @@ Return nil if CLI (a `doom-cli') has no explicit documentation."
(push (car option) seen))))) (push (car option) seen)))))
;; Populate arguments ;; Populate arguments
(let* ((arglist (doom-cli-context-arguments context)) (let* ((arglist (doom-cli-context-arguments context))
(rest (copy-sequence (alist-get (doom-cli-command cli) arglist nil nil #'equal))) (rest (copy-sequence (map-elt arglist (doom-cli-command cli))))
(args (copy-sequence (alist-get t arglist))) (args (copy-sequence (alist-get t arglist)))
(argc (length args)) (argc (length args))
(required (alist-get '&required argspec)) (required (alist-get '&required argspec))
@ -765,25 +758,31 @@ executable context."
((when-let* ((when-let*
(((null arguments)) (((null arguments))
((not rest?)) ((not rest?))
(command (append (doom-cli--command context) (list arg))) (command (append (doom-cli-context-command context) (list arg)))
(cli (doom-cli-get command t)) (cli (doom-cli-get command t))
(rcli (doom-cli-get command)) (rcli (doom-cli-get command))
(key (doom-cli-key rcli))) (key (doom-cli-key rcli)))
(doom-log "doom-cli-context-execute: found %s" command) (doom-log "doom-cli-context-execute: found %s" command)
;; Show warnings depending on CLI plists
(when (doom-cli-alias cli) (when (doom-cli-alias cli)
(dolist (pcli (doom-cli-path cli)) (dolist (pcli (doom-cli-path cli))
(doom-log "doom-cli-context-execute: path=%s" (doom-cli-key pcli)) (doom-log "doom-cli-context-execute: path=%s" (doom-cli-key pcli))
(push (doom-cli-key pcli) (doom-cli-context-path context)))) (push (doom-cli-key pcli) (doom-cli-context-path context))))
;; Collect &rest for this command
(setf (doom-cli-context-command context) key (setf (doom-cli-context-command context) key
(map-elt (doom-cli-context-arguments context) (map-elt (doom-cli-context-arguments context)
(doom-cli-command rcli)) (doom-cli-command rcli))
(copy-sequence args)) (copy-sequence args))
;; Initialize options associated with this command to a nil value;
;; this simplifies existence validation later.
(dolist (cli (doom-cli-find key)) (dolist (cli (doom-cli-find key))
(dolist (option (doom-cli-options cli)) (dolist (option (doom-cli-options cli))
(dolist (switch (doom-cli-option-switches option)) (dolist (switch (doom-cli-option-switches option))
(unless (assoc switch (doom-cli-context-options context)) (unless (assoc switch (doom-cli-context-options context))
(setf (map-elt (doom-cli-context-options context) switch) (setf (map-elt (doom-cli-context-options context) switch)
nil))))) nil)))))
;; If this command uses &rest, stop processing commands from this
;; point on and pass the rest (of the unprocessed arguments) to it.
(when (and (doom-cli-fn rcli) (when (and (doom-cli-fn rcli)
(alist-get '&rest (doom-cli-arguments rcli))) (alist-get '&rest (doom-cli-arguments rcli)))
(setq rest? t)) (setq rest? t))
@ -975,14 +974,6 @@ See `doom-cli-log-file-format' for details."
(princ str buffer))) (princ str buffer)))
(send-string-to-terminal str))) (send-string-to-terminal str)))
(defun doom-cli--output-read-stdin (buffer)
(with-current-buffer buffer
(let (in)
(while (setq in (ignore-errors (read-from-minibuffer "")))
(insert in "\n"))
(when in
(delete-char -1)))))
(defun doom-cli--output-write-logs-h (context) (defun doom-cli--output-write-logs-h (context)
"Write all log buffers to their appropriate files." "Write all log buffers to their appropriate files."
;; Delete the last `doom-cli-log-retain' logs ;; Delete the last `doom-cli-log-retain' logs
@ -1699,7 +1690,10 @@ errors to `doom-cli-error-file')."
(when (doom-cli-context-pipe-p context :out t) (when (doom-cli-context-pipe-p context :out t)
(setq doom-print-backend nil)) (setq doom-print-backend nil))
(when (doom-cli-context-pipe-p context :in) (when (doom-cli-context-pipe-p context :in)
(doom-cli--output-read-stdin (doom-cli-context-stdin context))) (with-current-buffer (doom-cli-context-stdin context)
(while (if-let (in (ignore-errors (read-from-minibuffer "")))
(insert in "\n")
(ignore-errors (delete-char -1))))))
(doom-log "doom-cli-run: %s" command-line-args) (doom-log "doom-cli-run: %s" command-line-args)
(doom-cli--exit (doom-cli--exit
(condition-case e (condition-case e