[#213] Allow -e argument to be repeated

This commit is contained in:
Michiel Borkent 2020-01-20 11:00:53 +01:00
parent 8f698894f6
commit a442bde76a
2 changed files with 14 additions and 12 deletions

View file

@ -99,21 +99,21 @@
("--eval", "-e") ("--eval", "-e")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map :expression (first options)))) (update opts-map :expressions (fnil conj []) (first options))))
("--main", "-m") ("--main", "-m")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map :main (first options)))) (assoc opts-map :main (first options))))
(if (some opts-map [:file :socket-repl :expression :main]) (if (some opts-map [:file :socket-repl :expressions :main])
(assoc opts-map (assoc opts-map
:command-line-args options) :command-line-args options)
(let [trimmed-opt (str/triml opt) (let [trimmed-opt (str/triml opt)
c (.charAt trimmed-opt 0)] c (.charAt trimmed-opt 0)]
(case c (case c
(\( \{ \[ \* \@ \#) (\( \{ \[ \* \@ \#)
(assoc opts-map (-> opts-map
:expression opt (update :expressions (fnil conj []) (first options))
:command-line-args (next options)) (assoc :command-line-args (next options)))
(assoc opts-map (assoc opts-map
:file opt :file opt
:command-line-args (next options))))))) :command-line-args (next options)))))))
@ -274,7 +274,7 @@ Everything after that is bound to *command-line-args*."))
(let [t0 (System/currentTimeMillis) (let [t0 (System/currentTimeMillis)
{:keys [:version :shell-in :edn-in :shell-out :edn-out {:keys [:version :shell-in :edn-in :shell-out :edn-out
:help? :file :command-line-args :help? :file :command-line-args
:expression :stream? :time? :expressions :stream? :time?
:repl :socket-repl :repl :socket-repl
:verbose? :classpath :verbose? :classpath
:main :uberscript] :as _opts} :main :uberscript] :as _opts}
@ -348,13 +348,14 @@ Everything after that is bound to *command-line-args*."))
(let [opts (apply hash-map opts)] (let [opts (apply hash-map opts)]
(repl/start-repl! sci-ctx opts)))))) (repl/start-repl! sci-ctx opts))))))
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
[expression exit-code] [expressions exit-code]
(cond expression [expression nil] (cond expressions [expressions nil]
main [(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)"
main) nil] main)] nil]
file (try [(read-file file) nil] file (try [[(read-file file)] nil]
(catch Exception e (catch Exception e
(error-handler* e verbose?)))) (error-handler* e verbose?))))
expression (str/join " " expressions) ;; this might mess with the locations...
exit-code exit-code
;; handle preloads ;; handle preloads
(if exit-code exit-code (if exit-code exit-code

View file

@ -19,7 +19,8 @@
(testing "distinguish automatically between expression or file name" (testing "distinguish automatically between expression or file name"
(is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb"))) (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb")))
(is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj")))
(is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))
(is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])"))))
(deftest print-error-test (deftest print-error-test
(is (thrown-with-msg? Exception #"java.lang.NullPointerException" (is (thrown-with-msg? Exception #"java.lang.NullPointerException"