[#219] better error message for unrecognized cli option

This commit is contained in:
Michiel Borkent 2020-01-12 17:27:17 +01:00
parent 1268792b47
commit 8e24744d35
2 changed files with 17 additions and 27 deletions

View file

@ -107,14 +107,16 @@
(if (some opts-map [:file :socket-repl :expression :main]) (if (some opts-map [:file :socket-repl :expression :main])
(assoc opts-map (assoc opts-map
:command-line-args options) :command-line-args options)
(if (and (not= \( (first (str/trim opt))) (let [opt (str/triml opt)
(.exists (io/file opt))) c (.charAt opt 0)]
(assoc opts-map (case c
:file opt (\( \{ \[ \* \@ \#)
:command-line-args (next options)) (assoc opts-map
(assoc opts-map :expression opt
:expression opt :command-line-args (next options))
:command-line-args (next options)))))) (assoc opts-map
:file opt
:command-line-args (next options)))))))
opts-map))] opts-map))]
opts)) opts))

View file

@ -13,24 +13,13 @@
(edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args)))) (edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args))))
(deftest parse-opts-test (deftest parse-opts-test
(is (= {:expression "(println 123)"} (is (= 123 (bb nil "(println 123)")))
(main/parse-opts ["-e" "(println 123)"]))) (is (= 123 (bb nil "-e" "(println 123)")))
(is (= 123 (bb nil "--eval" "(println 123)")))
(is (= {:expression "(println 123)"}
(main/parse-opts ["--eval" "(println 123)"])))
(testing "distinguish automatically between expression or file name" (testing "distinguish automatically between expression or file name"
(is (= {:expression "(println 123)" (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb")))
:command-line-args nil} (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj")))
(main/parse-opts ["(println 123)"]))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help")))))
(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"])))))
(deftest main-test (deftest main-test
(testing "-io behaves as identity" (testing "-io behaves as identity"
@ -250,8 +239,7 @@
(deftest Pattern-test (deftest Pattern-test
(is (= ["1" "2" "3"] (is (= ["1" "2" "3"]
(bb nil "(vec (.split (java.util.regex.Pattern/compile \"f\") \"1f2f3\"))"))) (bb nil "(vec (.split (java.util.regex.Pattern/compile \"f\") \"1f2f3\"))")))
(is (= java.util.regex.Pattern/CANON_EQ (is (true? (bb nil "(some? java.util.regex.Pattern/CANON_EQ)"))))
(bb nil "java.util.regex.Pattern/CANON_EQ"))))
(deftest writer-test (deftest writer-test
(let [tmp-file (java.io.File/createTempFile "bbb" "bbb") (let [tmp-file (java.io.File/createTempFile "bbb" "bbb")