code / clarence / main.lisp

1;;;; Entry point for the clarence application
2;; Clarence is a unix filter, meaning it consumes a stream and outputs a
3;; transformed stream.
4
5(defpackage :clarence/main
6  (:use :cl :net.acdw.clarence)
7  (:export #:main))
8
9(in-package :clarence/main)
10
11(defun clarence/command ()
12  "Creates and returns the top-level command."
13  (clingon:make-command
14   :name "clarence"
15   :description "A gemtext converter for other formats"
16   :version "1780.4"
17   :license "BSD 3-Clause"
18   :authors '("Case Duckworth <acdw@acdw.net>")
19   :usage "[-t FORMAT] [-o FILE] [FILE]"
20   :options (clarence/options)
21   :handler #'clarence/handler))
22
23(defun clarence/options ()
24  "The command-line options for `clarence' command."
25  (list (clingon:make-option
26         :string
27         :description "Output format"
28         :short-name #\t
29         :long-name "to"
30         :initial-value "gemtext"
31         :key :to)
32        (clingon:make-option
33         :filepath
34         :description "Output file"
35         :short-name #\o
36         :long-name "output"
37         :key :output)))
38
39(defun clarence/handler (cmd)
40  "The top-level handler."
41  (let ((input-file (car (clingon:command-arguments cmd)))
42        (input-stream *standard-input*)
43        (output-format  (clingon:getopt cmd :to))
44        (output-file (clingon:getopt cmd :output))
45        (output-stream *standard-output*))
46    (when input-file
47      (setf input-stream (open input-file :direction :input)))
48    (when output-file
49      (setf output-stream (open output-file :direction :output)))
50    (princ (document-string (parse input-stream) :writer output-format)
51           output-stream)
52    (close input-stream)
53    (close output-stream)))
54
55(defun main ()
56  (let ((app (clarence/command)))
57    (clingon:run app)))