From 035a8268ba666afc24d0a3e14dd7c2d9fecf3cc0 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 26 May 2024 05:41:47 -0400 Subject: [PATCH] Fix #1695: make CLI args available to REPL (#1697) --- CHANGELOG.md | 2 ++ src/babashka/main.clj | 8 +++++--- test/babashka/main_test.clj | 10 +++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db2c54f2..82525ff3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ A preview of the next release can be installed from - Fix [#1688](https://github.com/babashka/babashka/issues/1688): use-fixtures should add metadata to `*ns*` - Fix [#1692](https://github.com/babashka/babashka/issues/1692): Add support for ITransientSet and org.flatland/ordered-set - Bump org.flatland/ordered to `1.15.12`. +- Partially Fix [#1695](https://github.com/babashka/babashka/issues/1695): `--repl` arg handling should consume only one arg (itself) ([@bobisageek](https://github.com/bobisageek)) +- Partially Fix [#1695](https://github.com/babashka/babashka/issues/1695): make `*command-line-args*` value available in the REPL ([@bobisageek](https://github.com/bobisageek)) ## 1.3.190 (2024-04-17) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 86dec93a..348802d8 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -613,7 +613,7 @@ Use bb run --help to show this help output. :uberjar (first options)))) ("--repl") (let [options (next options)] - (recur (next options) + (recur options (assoc opts-map :repl true))) ("--socket-repl") @@ -1021,7 +1021,8 @@ Use bb run --help to show this help output. doc (print-doc sci-ctx command-line-args) describe? [(print-describe) 0] - repl [(repl/start-repl! sci-ctx) 0] + repl (sci/binding [core/command-line-args command-line-args] + [(repl/start-repl! sci-ctx) 0]) nrepl [(start-nrepl! nrepl) 0] uberjar [nil 0] list-tasks [(tasks/list-tasks sci-ctx) 0] @@ -1069,7 +1070,8 @@ Use bb run --help to show this help output. clojure [nil (if-let [proc (bdeps/clojure command-line-args)] (-> @proc :exit) 0)] - :else [(repl/start-repl! sci-ctx) 0])) + :else (sci/binding [core/command-line-args command-line-args] + [(repl/start-repl! sci-ctx) 0]))) 1)] (flush) (when uberscript diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index b6e62947..26f67290 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -68,6 +68,10 @@ (is (= {:force? true :repl true} (parse-opts ["--force" "repl"]))) (is (= {:force? true :clojure true :command-line-args '("-M" "-r")} (parse-opts ["--force" "clojure" "-M" "-r"]))) + (is (= {:command-line-args '("asdf" "fdsa")} + (main/parse-opts ["--" "asdf" "fdsa"]))) + (is (= {:repl true :command-line-args '("asdf" "fdsa")} + (main/parse-opts ["repl" "--" "asdf" "fdsa"]))) (testing "file opts parsing does not mess with :command-line-args" (is (= {:prn true, :expressions ["(prn :foo)"]} (-> (let [[_ opts] (main/parse-file-opt ["-e" "(prn :foo)"] {})] @@ -476,7 +480,11 @@ (deftest command-line-args-test (is (true? (bb nil "(nil? *command-line-args*)"))) - (is (= ["1" "2" "3"] (bb nil "*command-line-args*" "1" "2" "3")))) + (is (= ["1" "2" "3"] (bb nil "*command-line-args*" "1" "2" "3"))) + (is (str/includes? (test-utils/bb "*command-line-args*" "repl" "--" "1" "2" "3") + "(\"1\" \"2\" \"3\"")) + (is (str/includes? (test-utils/bb "*command-line-args*" "--" "1" "2" "3") + "(\"1\" \"2\" \"3\""))) (deftest constructors-test (testing "the clojure.lang.Delay constructor works"