/
main.clj
54 lines (47 loc) · 1.65 KB
/
main.clj
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
(ns klipse-repl.main
(:require
[gadjett.core :refer [dbg]]
[klipse-repl.repl :refer [create-repl]]
[clojure.string :as string]
[clojure.tools.cli :refer [parse-opts]]))
(def cli-options
[["-h" "--help"]
[nil "--cool-forms"]
[nil "--[no-]rebel" "enable/disable rebel readline"
:default true]
[nil "--[no-]easy-defs" "enable/disable easy def feedback"
:default true]
["-p" "--port PORT" "Port Number for the socket repl"
:parse-fn #(Integer/parseInt %)
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]])
(defn usage [options-summary]
(->> ["klipse-repl: A beginner friendly REPL"
"Usage: clj -A:klipse-repl [options]"
""
"Options:"
options-summary]
(string/join \newline)))
(defn error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(string/join \newline errors)))
(defn validate-args
"Validate command line arguments. Either return a map indicating the program
should exit (with a error message, and optional ok status), or a map
indicating the action the program should take and the options provided."
[args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
(cond
(:help options)
{:exit-message (usage summary) :ok? true}
errors
{:exit-message (error-msg errors)}
:else
{:action "do" :options options})))
(defn exit [status msg]
(println msg)
(System/exit status))
(defn -main [& args]
(let [{:keys [action options exit-message ok?]} (validate-args args)]
(if exit-message
(exit (if ok? 0 1) exit-message)
(create-repl options))))