From 9a9f629068b4ffe7173bc92f12080685743dc6ab Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 5 Aug 2022 09:50:42 -0500 Subject: [add] defhandler macro for app package. --- app/util.lisp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'app/util.lisp') diff --git a/app/util.lisp b/app/util.lisp index d9d5ea2..70dee49 100644 --- a/app/util.lisp +++ b/app/util.lisp @@ -9,3 +9,30 @@ (when str (or (parse-integer str :junk-allowed t) str))) + +(defvar *cmd* nil) + +(defmacro defhandler (name args-pattern &body body) + "Convenience macro for defining command handlers that take +arguments. The args pattern list of forms sutable for passint to +destructuring-bind. + +Also binds *cmd* to the current command, so that you can pass it to +getopt forms as needed. + +E.g. (defhandler moo (a b . more) ...) + +would become (destructuring-bind. (a b . more) (command-argumets *cmd*) ...) + +If an error occurs at any time during the execution of BODY, then it +is caught and handled by printing the usage statemnnt for the current +command. +" + (let ((cmd-var (gensym "cmd"))) + `(defun ,name (,cmd-var) + (let ((*cmd* ,cmd-var)) + (handler-case + (destructuring-bind ,args-pattern (clingon:command-arguments *cmd*) + ,@body) + (error () + (clingon:print-usage-and-exit *cmd* t))))))) -- cgit v1.2.3