-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrono.lisp
74 lines (61 loc) · 2.36 KB
/
chrono.lisp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;;;; chrono.lisp
;(eval-when (:compile-toplevel :load-toplevel :execute)
; (ql:quickload '(:with-user-abort :chronicity :adopt) :silent t))
(in-package :chrono)
;;;; Configuration -----------------------------------------------
(defparameter *whatever* 123)
;;;; Errors ------------------------------------------------------
(define-condition user-error (error) ())
(define-condition missing-foo (user-error) ()
(:report "A foo is required, but none was supplied."))
(define-condition invalid-input-string (user-error)
((input-string
:initarg :input-string
:reader get-input-string))
(:report (lambda (condition stream)
(format stream "Invalid input string: ~d." (get-input-string condition)))))
;;;; Functionality -----------------------------------------------
(defun handle-input-string (string)
(let ((time (chronicity:parse string)))
(if (not (null time))
(format t "~d~%" (local-time:format-timestring nil time :format local-time:+rfc-1123-format+))
(error (make-condition 'invalid-input-string :input-string string)))))
;;;; Run ---------------------------------------------------------
(defun run (arguments)
(map nil #'handle-input-string arguments))
;;;; User Interface ----------------------------------------------
(defmacro exit-on-ctrl-c (&body body)
`(handler-case (with-user-abort:with-user-abort (progn ,@body))
(with-user-abort:user-abort () (sb-ext:exit :code 130))))
(defparameter *option-help*
(adopt:make-option 'help
:long "help"
:short #\h
:help "Display help and exit."
:reduce (constantly t)))
(defparameter *option-version*
(adopt:make-option 'version
:long "version"
:short #\v
:help "Display version and exit."
:reduce (constantly t)))
(defparameter *ui*
(adopt:make-interface
:name "chrono"
:summary "yada yada yada"
:usage "hello, world"
:help "help me!"
:contents (list *option-help*
*option-version*)))
(defun toplevel (args)
(sb-ext:disable-debugger)
(exit-on-ctrl-c
(multiple-value-bind (arguments options) (adopt:parse-options-or-exit *ui* (cdr args))
; Handle options.
(when (gethash 'help options)
(adopt:print-help-and-exit *ui*))
(when (gethash 'version options)
(format t "1.0.0~%")
(adopt:exit))
(handler-case (run arguments)
(user-error (e) (adopt:print-error-and-exit e))))))