From 4792097ca56f278344f18a0a78aaca1278fd146e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 27 Feb 2021 19:12:07 +0100 Subject: Move everything from `l/` into the project root This is the final project. Now that we got rid of the web extension and native host code, we can move the Lisp code to the root. --- src/option.lisp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/option.lisp (limited to 'src/option.lisp') diff --git a/src/option.lisp b/src/option.lisp new file mode 100644 index 0000000..6f9129c --- /dev/null +++ b/src/option.lisp @@ -0,0 +1,82 @@ +;;;; Command line options. + +(in-package :extreload) + +;; Available command line options. +(opts:define-opts + (:name :socket-url + :description "DevTools protocol WebSocket URL" + :long "socket-url" + :arg-parser #'identity + :meta-var "SOCKET_URL") + (:name :reload-current-tab + :description "pass this to reload the active Chrome tab" + :long "reload-current-tab") + (:name :debug + :description "print debug output" + :long "debug") + (:name :help + :description "print this help menu" + :short #\h + :long "help") + (:name :version + :description "show the program version" + :short #\V + :long "version")) + +(defmacro when-option ((options option) &body body) + "When `option` is present in `options`, run `body`." + `(let ((value (getf ,options ,option))) + (when value + ,@body))) + +(defun exit-with-error (condition exit-code) + "Print the error associated with `condition` on standard error, then exit +with code `exit-code`." + (format *error-output* "error: ~a~%" condition) + + (opts:exit exit-code)) + +(defun handle-option-error (condition) + "Handle errors related to command line options. Prints the error specified by +`condition` and exits with EX_USAGE." + (exit-with-error condition sysexits:+usage+)) + +(defun parse-options () + "Parse command line options." + (multiple-value-bind (options free-args) + (handler-bind + ((opts:unknown-option #'handle-option-error) + (opts:missing-arg #'handle-option-error) + (opts:arg-parser-failed #'handle-option-error) + (opts:missing-required-option #'handle-option-error)) + + (opts:get-opts)) + + (when-option (options :help) + (opts:describe + :usage-of "extreload" + :args "EXTENSION_ID...") + + (opts:exit sysexits:+usage+)) + + (when-option (options :version) + (format t "~a~%" (asdf:component-version (asdf:find-system :extreload))) + + (opts:exit sysexits:+ok+)) + + (when (null (getf options :socket-url)) + (format *error-output* "error: '--socket-url' is required~%") + + (opts:exit sysexits:+usage+)) + + ;; Error if no extension IDs were given. + (when (null free-args) + (format *error-output* "error: missing extension IDs~%") + + (opts:exit sysexits:+usage+)) + + (make-config :socket-url (getf options :socket-url) + :reload-current-tab (getf options :reload-current-tab) + :debug-output (getf options :debug) + :extension-ids free-args))) -- cgit v1.2.3