/
commandline.clj
79 lines (58 loc) · 1.64 KB
/
commandline.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
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
(ns fif.commandline
"Used for the standalone fif executable."
(:require
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.tools.cli :refer [parse-opts]]
[fif.core :as fif]
[fif.def :refer [set-word-variable]]
[fif.repl]
[fif.stdlib.io :refer [import-stdlib-io]])
(:gen-class))
(def ^:dynamic *commandline-stack*
(-> fif/*default-stack*
import-stdlib-io))
(def help-message
"fif Language Commandline repl/eval
Usage:
fif [options]
fif <filename> [arguments..] [options]
Options:
-h, --help Show this screen.
-e Evaluate Commandline Arguments
Website:
github.com/benzap/fif
Notes:
* Commandline Arguments are placed in the word variable $vargs
* The :stdlib.io group includes additional io operations for reading
and writing files")
(def cli-options
[["-h" "--help"]
["-e" nil :id :eval]])
(defn -main
[& args]
(let [{:keys [options arguments errors]}
(parse-opts args cli-options)]
(cond
(not (empty? errors))
(do
(println "CMDERROR")
(doseq [errstr errors]
(println errstr))
(println)
(println help-message))
(:help options)
(println help-message)
(:eval options)
(fif/with-stack *commandline-stack*
(fif/eval-string (str/join " " arguments))
(flush))
(> (count arguments) 0)
(let [[filename & args] arguments
sform (slurp filename)
sm (set-word-variable *commandline-stack* '$vargs (vec args))]
(fif/with-stack sm
(fif/eval-string sform)
(flush)))
:else
(fif.repl/repl *commandline-stack*))))