From 8e24744d351be5d869e6bffb204682c2f82b17cc Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 17:27:17 +0100 Subject: [PATCH] [#219] better error message for unrecognized cli option --- src/babashka/main.clj | 18 ++++++++++-------- test/babashka/main_test.clj | 26 +++++++------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index b2c5faef..8606a932 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -107,14 +107,16 @@ (if (some opts-map [:file :socket-repl :expression :main]) (assoc opts-map :command-line-args options) - (if (and (not= \( (first (str/trim opt))) - (.exists (io/file opt))) - (assoc opts-map - :file opt - :command-line-args (next options)) - (assoc opts-map - :expression opt - :command-line-args (next options)))))) + (let [opt (str/triml opt) + c (.charAt opt 0)] + (case c + (\( \{ \[ \* \@ \#) + (assoc opts-map + :expression opt + :command-line-args (next options)) + (assoc opts-map + :file opt + :command-line-args (next options))))))) opts-map))] opts)) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 0e90881b..f622fa72 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -13,24 +13,13 @@ (edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args)))) (deftest parse-opts-test - (is (= {:expression "(println 123)"} - (main/parse-opts ["-e" "(println 123)"]))) - - (is (= {:expression "(println 123)"} - (main/parse-opts ["--eval" "(println 123)"]))) - + (is (= 123 (bb nil "(println 123)"))) + (is (= 123 (bb nil "-e" "(println 123)"))) + (is (= 123 (bb nil "--eval" "(println 123)"))) (testing "distinguish automatically between expression or file name" - (is (= {:expression "(println 123)" - :command-line-args nil} - (main/parse-opts ["(println 123)"]))) - - (is (= {:file "src/babashka/main.clj" - :command-line-args nil} - (main/parse-opts ["src/babashka/main.clj"]))) - - (is (= {:expression "does-not-exist" - :command-line-args nil} - (main/parse-opts ["does-not-exist"]))))) + (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 "-help"))))) (deftest main-test (testing "-io behaves as identity" @@ -250,8 +239,7 @@ (deftest Pattern-test (is (= ["1" "2" "3"] (bb nil "(vec (.split (java.util.regex.Pattern/compile \"f\") \"1f2f3\"))"))) - (is (= java.util.regex.Pattern/CANON_EQ - (bb nil "java.util.regex.Pattern/CANON_EQ")))) + (is (true? (bb nil "(some? java.util.regex.Pattern/CANON_EQ)")))) (deftest writer-test (let [tmp-file (java.io.File/createTempFile "bbb" "bbb")