1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-19 15:14:48 +00:00
janet/src/mainclient/init.janet

61 lines
1.9 KiB
Plaintext
Raw Normal View History

# Copyright 2017-2018 (C) Calvin Rose
2018-08-04 01:51:35 +00:00
(do
(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)
# Flag handlers
2018-08-16 02:48:35 +00:00
(def handlers :private
{"h" (fn [&]
(print "usage: " process/args.0 " [options] scripts...")
2018-08-16 02:48:35 +00:00
(print
`Options are:
-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
-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)
-- Stop handling options`)
(os/exit 0)
1)
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
"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 &]
(:= *no-file* false)
(eval (get process/args (+ i 1)))
2)})
(defn- dohandler [n i &]
(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")))))
# Process arguments
(var i 1)
(def lenargs (length process/args))
(while (< i lenargs)
(def arg (get process/args i))
(if (and *handleopts* (= "-" (string/slice arg 0 1)))
(+= i (dohandler (string/slice arg 1 2) i))
(do
(:= *no-file* false)
(import* _env arg :prefix "" :exit *exit-on-error*)
(++ i))))
2018-08-16 02:48:35 +00:00
(when (or *should-repl* *no-file*)
(if *raw-stdin*
(repl nil identity)
(do
(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]
(def offset (parser/where p))
(def prompt (string "janet:" offset ":" (parser/state p) "> "))
(getline prompt buf)))))))