Rewrite core-cli
Highlights: - 'doom purge' now purges builds, elpa packages, and repos by default. Regrafting repos is now opt-in with the -g/--regraft switches. Negation flags have been added for elpa/repos: -e/--no-elpa and -r/--no-repos. - Removed 'doom rebuild' (it is now just 'doom build' or 'doom b'). - Removed 'doom build's -f flag, this is now the default. Added the -r flag instead, which only builds packages that need rebuilding. - 'doom update' now updates packages synchronously, but produces more informative output about the updating process. - Straight can now prompt in batch mode, which resolves a lot of issues with 'doom update' (and 'doom upgrade') freezing indefinitely or throwing repo branch errors. - 'bin/doom's switches are now positional. Switches aimed at `bin/doom` must precede any subcommands. e.g. Do: 'doom -yd upgrade' Don't do: 'doom upgrade -yd' - Moved 'doom doctor' from bin/doom-doctor to core/cli/doctor, and integrated core/doctor.el into it, as to avoid naming conflicts between it and Emacs doctor. - The defcli! macro now has a special syntax for declaring flags, their arguments and descriptions. Addresses #1981, #1925, #1816, #1721, #1322
This commit is contained in:
parent
99cd52e70f
commit
873fc5c0db
16 changed files with 996 additions and 1266 deletions
|
@ -124,89 +124,3 @@ Warning: freezes indefinitely on any stdin prompt."
|
|||
(sit-for 0.1))
|
||||
(process-exit-status process))
|
||||
(string-trim (buffer-string)))))
|
||||
|
||||
(defun doom--cli-normalize (args specs)
|
||||
(let* ((args (cl-remove-if-not #'stringp args))
|
||||
(optspec (cl-remove-if-not #'listp specs))
|
||||
(argspec (cl-remove-if #'listp specs))
|
||||
(options (mapcar #'list (mapcar #'car-safe optspec)))
|
||||
extra
|
||||
arguments)
|
||||
(dolist (spec optspec)
|
||||
(setf (nth 1 spec) (doom-enlist (nth 1 spec))))
|
||||
(while args
|
||||
(let ((arg (pop args)))
|
||||
(cl-check-type arg string)
|
||||
(if (not (string-prefix-p "-" arg))
|
||||
(push arg arguments)
|
||||
(if-let (specs (cl-remove-if-not
|
||||
(if (string-prefix-p "--" arg)
|
||||
(doom-partial #'member arg)
|
||||
(lambda (flags)
|
||||
(cl-loop for switch in (split-string (string-remove-prefix "-" arg) "" t)
|
||||
if (member (concat "-" switch) flags)
|
||||
return t)))
|
||||
optspec
|
||||
:key #'cadr))
|
||||
(pcase-dolist (`(,sym ,flags ,type) specs)
|
||||
(setf (alist-get sym options)
|
||||
(list
|
||||
(let ((value (if type (pop args))))
|
||||
(pcase type
|
||||
(`&string value)
|
||||
(`&int `(truncate (read ,value)))
|
||||
(`&float `(float (read ,value)))
|
||||
(`&path `(expand-file-name ,value))
|
||||
(`&directory
|
||||
`(let ((path (expand-file-name ,value)))
|
||||
(unless (file-directory-p path)
|
||||
(error "Directory does not exist: %s" path))
|
||||
path))
|
||||
(`&file
|
||||
`(let ((path (expand-file-name ,value)))
|
||||
(unless (file-exists-p path)
|
||||
(error "File does not exist: %s" path))
|
||||
path))
|
||||
(`&sexp `(read ,value))
|
||||
((or `nil `t) arg)
|
||||
(_ (error "Not a valid type: %S" type)))))))
|
||||
(push arg extra)))))
|
||||
(list optspec (nreverse options)
|
||||
argspec (nreverse arguments))))
|
||||
|
||||
;;;###autoload
|
||||
(defun doom-cli-getopts (args specs)
|
||||
"TODO"
|
||||
(cl-destructuring-bind (optspec options argspec arguments)
|
||||
(doom--cli-normalize args specs)
|
||||
(let ((i 0)
|
||||
optional-p
|
||||
noerror-p)
|
||||
(cl-dolist (spec argspec)
|
||||
(cond ((eq spec '&rest)
|
||||
(push (list (cadr (member '&rest specs))
|
||||
`(quote
|
||||
,(reverse
|
||||
(butlast (reverse arguments) i))))
|
||||
options)
|
||||
(cl-return))
|
||||
((eq spec '&all)
|
||||
(push (list (cadr (member '&all specs))
|
||||
`(quote ,args))
|
||||
options))
|
||||
((eq spec '&noerror) (setq noerror-p t))
|
||||
((eq spec '&optional) (setq optional-p t))
|
||||
((and (>= i (length arguments)) (not optional-p))
|
||||
(signal 'wrong-number-of-arguments
|
||||
(list argspec (length arguments))))
|
||||
((push (list spec (nth i arguments)) options)
|
||||
(cl-incf i)))))
|
||||
(nreverse options)))
|
||||
|
||||
;;;###autoload
|
||||
(defmacro let-cliopts! (args spec &rest body)
|
||||
"Run BODY with command line ARGS parsed according to SPEC."
|
||||
(declare (indent 2))
|
||||
`(eval (append (list 'let (doom-cli-getopts ,args ',spec))
|
||||
(quote ,body))
|
||||
t))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue