aboutsummaryrefslogtreecommitdiff
path: root/app/util.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'app/util.lisp')
-rw-r--r--app/util.lisp27
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)))))))