forked from tshatrov/ichiran
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.lisp
109 lines (93 loc) · 3.15 KB
/
cli.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(defpackage :ichiran/cli
(:use :cl :ichiran/all)
(:export :build)
)
(in-package :ichiran/cli)
(opts:define-opts
(:name :help
:description "print this help text"
:short #\h
:long "help")
(:name :eval
:description "evaluate arbitrary expression and print the result"
:short #\e
:long "eval")
(:name :info
:description "print dictionary info"
:short #\i
:long "with-info")
(:name :full
:description "full split info (as JSON)"
:short #\f
:long "full"))
(defun unknown-option (condition)
(format t "warning: ~s option is unknown!~%" (opts:option condition))
(invoke-restart 'opts:skip-option))
(defun print-error (fmt &rest args)
(apply 'format *error-output* fmt args)
(finish-output *error-output*))
(defmethod jsown:to-json ((word-info word-info))
(jsown:to-json (word-info-gloss-json word-info)))
(defun print-romanize-info (info)
(loop for (word . gloss) in info
do (format t "~%~%* ~a ~a" word gloss)))
(defun main ()
(load-connection-from-env)
(multiple-value-bind (options free-args)
(handler-case
(handler-bind ((opts:unknown-option #'unknown-option))
(opts:get-opts))
(opts:missing-arg (condition)
(print-error "fatal: option ~s needs an argument!~%"
(opts:option condition))
(opts:exit 1))
(opts:arg-parser-failed (condition)
(print-error "fatal: cannot parse ~s as argument of ~s~%"
(opts:raw-arg condition)
(opts:option condition))
(opts:exit 1))
(opts:missing-required-option (con)
(print-error "fatal: ~a~%" con)
(opts:exit 1)))
(cond
((getf options :help)
(opts:describe
:prefix "Command line interface for Ichiran"
:suffix "By default calls ichiran:romanize, other options change this behavior"
:usage-of "ichiran-cli"
:args "[input]"))
((getf options :eval)
(let ((input (car free-args)))
(use-package :ichiran/all)
(mapcar 'print (multiple-value-list (eval (read-from-string input))))))
((getf options :info)
(let ((input (join " " free-args)))
(multiple-value-bind (r info) (romanize input :with-info t)
(princ r)
(print-romanize-info info))))
((getf options :full)
(let* ((input (join " " free-args))
(result (romanize* input :limit 1))) ;; TODO: option for limit > 1
(princ (jsown:to-json result))))
(t (let ((input (join " " free-args)))
(princ (romanize input :with-info t))))
))
(terpri)
(finish-output))
(defun setup-debugger ()
(setf *debugger-hook*
(lambda (condition old-hook)
(declare (ignore old-hook))
(print-error "ERROR: ~a" condition)
(opts:exit 2))))
(defun build (&key conn debug)
(when conn
(switch-conn-vars conn))
(format t "Initializing caches~%")
(init-all-caches)
(init-suffixes t)
;; remove references to db connections which become obsolete once the image is loaded
(postmodern:clear-connection-pool)
(unless debug
(setup-debugger))
(asdf:make :ichiran/cli))