2018-05-18 03:41:20 +00:00
|
|
|
# Copyright 2017-2018 (C) Calvin Rose
|
2018-08-04 01:51:35 +00:00
|
|
|
|
2018-03-28 17:50:06 +00:00
|
|
|
(do
|
|
|
|
|
2018-07-02 01:12:46 +00:00
|
|
|
(var *should-repl* :private false)
|
|
|
|
(var *no-file* :private true)
|
|
|
|
(var *raw-stdin* :private false)
|
|
|
|
(var *handleopts* :private true)
|
|
|
|
(var *exit-on-error* :private true)
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2018-07-02 01:12:46 +00:00
|
|
|
# Flag handlers
|
2018-08-16 02:48:35 +00:00
|
|
|
(def handlers :private
|
2018-11-25 19:03:00 +00:00
|
|
|
{"h" (fn [&]
|
2018-12-01 03:49:21 +00:00
|
|
|
(print "usage: " process/args.0 " [options] scripts...")
|
2018-08-16 02:48:35 +00:00
|
|
|
(print
|
2018-07-02 01:12:46 +00:00
|
|
|
`Options are:
|
2018-05-26 18:17:44 +00:00
|
|
|
-h Show this help
|
|
|
|
-v Print the version string
|
|
|
|
-s Use raw stdin instead of getline like functionality
|
2018-09-06 02:18:42 +00:00
|
|
|
-e Execute a string of janet
|
2018-05-26 18:17:44 +00:00
|
|
|
-r Enter the repl after running all scripts
|
2018-06-08 19:58:23 +00:00
|
|
|
-p Keep on executing if there is a top level error (persistent)
|
2018-05-26 18:17:44 +00:00
|
|
|
-- Stop handling options`)
|
2018-12-01 03:49:21 +00:00
|
|
|
(os/exit 0)
|
2018-07-02 01:12:46 +00:00
|
|
|
1)
|
2018-12-08 19:17:03 +00:00
|
|
|
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
|
2018-11-25 19:03:00 +00:00
|
|
|
"s" (fn [&] (:= *raw-stdin* true) (:= *should-repl* true) 1)
|
|
|
|
"r" (fn [&] (:= *should-repl* true) 1)
|
|
|
|
"p" (fn [&] (:= *exit-on-error* false) 1)
|
|
|
|
"-" (fn [&] (:= *handleopts* false) 1)
|
|
|
|
"e" (fn [i &]
|
2018-07-02 01:12:46 +00:00
|
|
|
(:= *no-file* false)
|
2018-12-01 03:49:21 +00:00
|
|
|
(eval (get process/args (+ i 1)))
|
2018-07-02 01:12:46 +00:00
|
|
|
2)})
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2018-11-25 19:03:00 +00:00
|
|
|
(defn- dohandler [n i &]
|
2018-07-02 01:12:46 +00:00
|
|
|
(def h (get handlers n))
|
2018-08-16 02:48:35 +00:00
|
|
|
(if h (h i) (do (print "unknown flag -" n) ((get handlers "h")))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-07-02 01:12:46 +00:00
|
|
|
# Process arguments
|
|
|
|
(var i 1)
|
2018-12-01 03:49:21 +00:00
|
|
|
(def lenargs (length process/args))
|
2018-07-02 01:12:46 +00:00
|
|
|
(while (< i lenargs)
|
2018-12-01 03:49:21 +00:00
|
|
|
(def arg (get process/args i))
|
|
|
|
(if (and *handleopts* (= "-" (string/slice arg 0 1)))
|
|
|
|
(+= i (dohandler (string/slice arg 1 2) i))
|
2018-07-02 01:12:46 +00:00
|
|
|
(do
|
|
|
|
(:= *no-file* false)
|
2018-09-29 14:58:57 +00:00
|
|
|
(import* _env arg :prefix "" :exit *exit-on-error*)
|
2018-07-02 01:12:46 +00:00
|
|
|
(++ i))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-08-16 02:48:35 +00:00
|
|
|
(when (or *should-repl* *no-file*)
|
2018-07-02 01:12:46 +00:00
|
|
|
(if *raw-stdin*
|
|
|
|
(repl nil identity)
|
|
|
|
(do
|
2018-12-08 19:17:03 +00:00
|
|
|
(print (string "Janet " janet/version "-" janet/build " Copyright (C) 2017-2018 Calvin Rose"))
|
2018-08-04 01:51:35 +00:00
|
|
|
(repl (fn [buf p]
|
2018-12-13 23:46:53 +00:00
|
|
|
(def offset (parser/where p))
|
|
|
|
(def prompt (string "janet:" offset ":" (parser/state p) "> "))
|
2018-07-02 01:12:46 +00:00
|
|
|
(getline prompt buf)))))))
|