From 7604512684aee5cdc3150e788efca5b72c4cb1fb Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Fri, 31 May 2024 15:48:43 -0500 Subject: [PATCH] Add error-reporting to UNIX cli client --- src/unix.lisp | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/unix.lisp b/src/unix.lisp index 601d030..3814273 100644 --- a/src/unix.lisp +++ b/src/unix.lisp @@ -41,21 +41,21 @@ ;; NIL → NIL (defun main () "Actual invocation of the program. This is what you should set as :toplevel." - (multiple-value-bind (opts free) (opts:get-opts) - (when-opt opts :help (help)) - (let* ((input-file (pathname (car free))) - (url-base (or (getf opts :url-base) (getf opts :download-dir) "mirror/")) - (download-dir (or (getf opts :download-dir) url-base))) - (when (not input-file) - (format *error-output* "No file provided.~%") - (help 2)) - - (format - 't - (mirror-img:mirror-img - input-file - download-dir - :url-dir url-base))))) + (error-print + 99 "" + (multiple-value-bind (opts free) (opts:get-opts) + (when-opt opts :help (help)) + (let* ((input-file (car free)) + (url-base (or (getf opts :url-base) (getf opts :download-dir) "mirror/")) + (download-dir (or (getf opts :download-dir) url-base))) + (when (not input-file) + (error-print 1 "No HTML file provided. Use --help for more information.")) + (format + 't + (mirror-img:mirror-img + (pathname input-file) + download-dir + :url-dir url-base)))))) (opts:define-opts (:name :help @@ -67,8 +67,12 @@ (:name :download-dir :description "directory for all mirrored files" :short #\d :long "downloads" - :arg-parser (lambda (dir) - (car (directory dir))))) + :arg-parser + (lambda (dir) + (handler-case + (ensure-directories-exist dir) + (error (c) + (error-print 11 "Could not access or create directory." c)))))) ;; number stream → nil (defun help (&optional (exit-code 0) (stream *standard-output*)) @@ -76,3 +80,9 @@ (unix-opts:describe :prefix "usage: mirror-img [-h] [-d DIR] [-b BASE] HTML_FILE" :stream stream) (unix-opts:exit exit-code)) + +;; number string condition → nil +(defun error-print (exit-code &optional message condition) + "Print an error-message and exit." + (format *error-output* "~@[~A~%~]~@[~A~%~]" message condition) + (unix-opts:exit exit-code))