diff options
author | Colin Okay <colin@cicadas.surf> | 2022-08-05 09:50:42 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-08-05 09:50:42 -0500 |
commit | 9a9f629068b4ffe7173bc92f12080685743dc6ab (patch) | |
tree | d62379dc9eabadfc6c7c0f907e16894511376c00 /app/util.lisp | |
parent | f116178dcf8b450c76400e2a0fbd2991f2c227b4 (diff) |
[add] defhandler macro for app package.
Diffstat (limited to 'app/util.lisp')
-rw-r--r-- | app/util.lisp | 27 |
1 files changed, 27 insertions, 0 deletions
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))))))) |