diff --git a/src/babashka/cli.clj b/src/babashka/cli.clj index 7bc3cdd0..9109931b 100644 --- a/src/babashka/cli.clj +++ b/src/babashka/cli.clj @@ -5,8 +5,12 @@ (declare resolve-task) +(defn error [msg exit] + (binding [*out* *err*] + (println msg) + {:exit-code exit})) + (defn parse-opts [options] - (prn :options options) (let [opts (loop [options options opts-map {}] (if options @@ -129,7 +133,7 @@ :command-line-args (next options)) (if (str/starts-with? opt ":") (resolve-task opt {:command-line-args (next options)}) - (throw (Exception. (str "File does not exist: " opt)))))))))) + (error (str "File does not exist: " opt) 1)))))))) opts-map))] opts)) @@ -142,5 +146,5 @@ (let [cmd-line-args (get task :babashka/args)] ;; this is for invoking babashka itself with command-line-args (parse-opts (seq (concat cmd-line-args command-line-args)))) - (throw (Exception. (str "No such task: " task))))) - (throw (Exception. (str "File does not exist: " task)))))) + (error (str "No such task: " task) 1))) + (error (str "File does not exist: " task) 1)))) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index ae4f36be..e3698e90 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -371,7 +371,8 @@ Use -- to separate script command line args from bb command line args. :repl :socket-repl :nrepl :verbose? :classpath :main :uberscript :describe? - :jar :uberjar :clojure :task] :as opts} + :jar :uberjar :clojure :task + :exit-code] :as opts} (cli/parse-opts args) _ (when clojure (if-let [proc (deps/clojure (:opts opts))] @@ -457,15 +458,16 @@ Use -- to separate script command line args from bb command line args. _ (vreset! common/ctx sci-ctx) preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) [expressions exit-code] - (cond expressions [expressions nil] - main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" - main)] nil] - file (try [[(read-file file)] nil] - (catch Exception e - (error-handler e {:expression expressions - :verbose? verbose? - :preloads preloads - :loader (:loader @cp/cp-state)})))) + (if exit-code [nil exit-code] + (cond expressions [expressions nil] + main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" + main)] nil] + file (try [[(read-file file)] nil] + (catch Exception e + (error-handler e {:expression expressions + :verbose? verbose? + :preloads preloads + :loader (:loader @cp/cp-state)}))))) expression (str/join " " expressions) ;; this might mess with the locations... exit-code ;; handle preloads diff --git a/test-resources/bb.edn b/test-resources/bb.edn index b41816ee..e283adaf 100644 --- a/test-resources/bb.edn +++ b/test-resources/bb.edn @@ -1 +1,2 @@ -{:tasks {:eval-plus {:babashka/args ["-e" "(+ 1 2 3)"]}}} +{:tasks {:eval-plus {:babashka/args ["-e" "(+ 1 2 3)"]} + :tree {:babashka/args [":clojure" "-Stree"]}}}