diff --git a/README.md b/README.md index 281f2b99..9793ec4c 100644 --- a/README.md +++ b/README.md @@ -321,7 +321,27 @@ $ bb '(slurp "https://www.clojure.org")' | bb '(subs *in* 0 50)' " | --socket-repl [host:]port )") +(def usage-string "Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] ( -e | -f | --socket-repl [host:]port )") (defn print-usage [] (println usage-string)) @@ -113,9 +121,12 @@ -o: write lines to stdout. -O: write EDN values to stdout. --stream: stream over lines or EDN values from stdin. Combined with -i or -I *in* becomes a single value per iteration. - --file or -f: read expressions from file instead of argument wrapped in an implicit do. + -e, --eval expression: evaluate an expression + -f, --file path: evaluate a file --socket-repl: start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --time: print execution time before exiting. + + If neither -e, -f, or --socket-repl are specified, then the first argument is treated as a file if it exists, or as an expression otherwise. ")) (defn read-file [file] diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 4dbb35f3..f6f52bbe 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -10,6 +10,26 @@ (defn bb [input & args] (edn/read-string (apply test-utils/bb (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)"]))) + + (testing "distinguish automatically between expression or file name" + (is (= {:expression "(println 123)" + :command-line-args []} + (main/parse-opts ["(println 123)"]))) + + (is (= {:file "src/babashka/main.clj" + :command-line-args []} + (main/parse-opts ["src/babashka/main.clj"]))) + + (is (= {:expression "does-not-exist" + :command-line-args []} + (main/parse-opts ["does-not-exist"]))))) + (deftest main-test (testing "-io behaves as identity" (= "foo\nbar\n" (test-utils/bb "foo\nbar\n" "-io" "*in*")))