(in-package #:wallpaper-switcher) (defparameter *usage* "Usage: wallpaper-switcher [options] Print an Openbox pipe menu listing the wallpapers in a directory. Options: -c, --config FILE Read settings from FILE (default: $XDG_CONFIG_HOME/wallpaper-switcher/config) -d, --directory DIR Wallpaper directory (default: ~/.wallpapers) -e, --command CMD Shell command to set a wallpaper; %f is the file -h, --help Show this help ") (defun parse-args (args) "Return a plist of the options in ARGS. Signals an error on bad input." (let ((opts '())) (loop while args do (let ((arg (pop args))) (flet ((value () (or (pop args) (error "option ~A needs a value" arg)))) (cond ((member arg '("-h" "--help") :test #'string=) (setf (getf opts :help) t)) ((member arg '("-c" "--config") :test #'string=) (setf (getf opts :config) (value))) ((member arg '("-d" "--directory") :test #'string=) (setf (getf opts :directory) (value))) ((member arg '("-e" "--command") :test #'string=) (setf (getf opts :command) (value))) (t (error "unknown argument ~S" arg)))))) opts)) (defun resolve-config (opts) "Defaults, then the config file, then command-line options." (let* ((explicit (getf opts :config)) (path (if explicit (expand-path explicit) (default-config-path)))) (when (and explicit (not (probe-file path))) (error "config file ~A does not exist" path)) (let ((config (load-config path))) (when (getf opts :directory) (setf (config-directory config) (getf opts :directory))) (when (getf opts :command) (setf (config-command config) (getf opts :command))) config))) (defun run (args) "Do the work for ARGS and return an exit code. Safe to call from a REPL." (handler-case (let ((opts (parse-args args))) (if (getf opts :help) (write-string *usage*) ;; Warnings go to stderr; stdout must contain only the menu XML. (handler-bind ((warning (lambda (w) (format *error-output* "wallpaper-switcher: ~A~%" w) (muffle-warning w)))) (render-menu (resolve-config opts)))) (finish-output) 0) (error (e) (format *error-output* "wallpaper-switcher: ~A~%" e) 1))) (defun main () "Entry point for the built executable." (uiop:quit (run (uiop:command-line-arguments))))