-
Notifications
You must be signed in to change notification settings - Fork 0
/
hddl-to-json-entrypoint.lisp
43 lines (41 loc) · 2.27 KB
/
hddl-to-json-entrypoint.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
(in-package :common-lisp-user)
(defun usage (program-name)
(format *error-output* "~a [input-file [output-file]]~%" program-name)
(format *error-output* "Translate input HDDL into output JSON.~%")
(format *error-output* "If no command line arguments are present, reads from stdin and writes to stdout.~%")
(format *error-output* "If one command line argument is present, reads from argument file and writes to the same name, but with \".json\" extension.~%")
(format *error-output* "If two command line arguments are present, reads from first argument file and writes to second argument file.~%")
(finish-output *error-output*))
(defun main (argv)
(let (output-name
(tmpfile (unless (= (length argv) 1)
(uiop:with-temporary-file (:pathname pn)
pn))))
(case (length argv)
(1 ; just the program name
(hddl-json:hddl-to-json *standard-input* *standard-output*))
(2 ; just the input file name
(let* ((filename (second argv))
(pathname (parse-namestring filename))
(outfile (merge-pathnames (make-pathname :type "json")
pathname)))
(unless (probe-file filename)
(uiop:die 1 "Unable to open input HDDL file ~a" filename))
(uiop:with-input-file (in pathname :if-does-not-exist :error)
(uiop:with-output-file (out tmpfile :if-exists :supersede)
(hddl-json:hddl-to-json in out)))
(setf output-name outfile)))
(3
(let ((filename (second argv))
(outfile (third argv)))
(unless (probe-file filename)
(uiop:die 1 "Unable to open input HDDL file ~a" filename))
(uiop:with-input-file (in filename :if-does-not-exist :error)
(uiop:with-output-file (out tmpfile :if-exists :supersede)
(hddl-json:hddl-to-json in out)))
(setf output-name outfile)))
(otherwise
(usage (first argv))
(uiop:die 1 "Incorrect invocation: ~a requires 0, 1 or 2 arguments.")))
(when output-name (uiop:with-output-file (str output-name :if-exists :supersede)
(uiop:run-program `("jq" "." ,(namestring tmpfile)) :output (list str :linewise t))))))