aboutsummaryrefslogtreecommitdiff
path: root/app/util.lisp
blob: 6bcf57796b2465e56fca699281fc3dc28687da1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;;;; utils.lisp

(in-package :oneliners.cli.app)

(defun parse-identifier (str)
  "If STR holds digits representing an integer, parse them. otherwise
  return the string.  Oneliner identifiers may be names or ID
  numbers. Returns NIL in the case that STR is NIL"
  (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 *cmd* t)))))))