Fix starting REPL when no expression is given

This commit is contained in:
Michiel Borkent 2020-01-01 23:27:25 +01:00
parent a9758b6170
commit 0362ecb7f6
2 changed files with 25 additions and 14 deletions

View file

@ -131,25 +131,28 @@ You may also download a binary from [Github](https://github.com/borkdude/babashk
``` shellsession ``` shellsession
Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] [--verbose] Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] [--verbose]
[ ( --classpath | -cp ) <cp> ] [ ( --main | -m ) <main-namespace> ] [ ( --classpath | -cp ) <cp> ] [ --uberscript <file> ]
( -e <expression> | -f <file> | --repl | --socket-repl [<host>:]<port> ) [ ( --main | -m ) <main-namespace> | -e <expression> | -f <file> |
[ --uberscript <file> ] [ arg* ] --repl | --socket-repl [<host>:]<port> ]
[ arg* ]
Options: Options:
--help, -h or -? Print this help text. --help, -h or -? Print this help text.
--version Print the current version of babashka. --version Print the current version of babashka.
-i Bind *input* to a lazy seq of lines from stdin. -i Bind *input* to a lazy seq of lines from stdin.
-I Bind *input* to a lazy seq of EDN values from stdin. -I Bind *input* to a lazy seq of EDN values from stdin.
-o Write lines to stdout. -o Write lines to stdout.
-O Write EDN values to stdout. -O Write EDN values to stdout.
--verbose Print entire stacktrace in case of exception. --verbose Print entire stacktrace in case of exception.
--stream Stream over lines or EDN values from stdin. Combined with -i or -I *input* becomes a single value per iteration. --stream Stream over lines or EDN values from stdin. Combined with -i or -I *input* becomes a single value per iteration.
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
-e, --eval <expr> Evaluate an expression. -e, --eval <expr> Evaluate an expression.
-f, --file <path> Evaluate a file. -f, --file <path> Evaluate a file.
-cp, --classpath Classpath to use. -cp, --classpath Classpath to use.
-m, --main <ns> Call the -main function from namespace with args. -m, --main <ns> Call the -main function from namespace with args.
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
--repl Start REPL --repl Start REPL
--socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --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. --time Print execution time before exiting.

View file

@ -134,9 +134,10 @@
(println (str "babashka v"(str/trim (slurp (io/resource "BABASHKA_VERSION")))))) (println (str "babashka v"(str/trim (slurp (io/resource "BABASHKA_VERSION"))))))
(def usage-string "Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] [--verbose] (def usage-string "Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] [--verbose]
[ ( --classpath | -cp ) <cp> ] [ ( --main | -m ) <main-namespace> ] [ ( --classpath | -cp ) <cp> ] [ --uberscript <file> ]
( -e <expression> | -f <file> | --repl | --socket-repl [<host>:]<port> ) [ ( --main | -m ) <main-namespace> | -e <expression> | -f <file> |
[ --uberscript <file> ] [ arg* ]") --repl | --socket-repl [<host>:]<port> ]
[ arg* ]")
(defn print-usage [] (defn print-usage []
(println usage-string)) (println usage-string))
@ -150,17 +151,19 @@
(println " (println "
--help, -h or -? Print this help text. --help, -h or -? Print this help text.
--version Print the current version of babashka. --version Print the current version of babashka.
-i Bind *input* to a lazy seq of lines from stdin. -i Bind *input* to a lazy seq of lines from stdin.
-I Bind *input* to a lazy seq of EDN values from stdin. -I Bind *input* to a lazy seq of EDN values from stdin.
-o Write lines to stdout. -o Write lines to stdout.
-O Write EDN values to stdout. -O Write EDN values to stdout.
--verbose Print entire stacktrace in case of exception. --verbose Print entire stacktrace in case of exception.
--stream Stream over lines or EDN values from stdin. Combined with -i or -I *input* becomes a single value per iteration. --stream Stream over lines or EDN values from stdin. Combined with -i or -I *input* becomes a single value per iteration.
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
-e, --eval <expr> Evaluate an expression. -e, --eval <expr> Evaluate an expression.
-f, --file <path> Evaluate a file. -f, --file <path> Evaluate a file.
-cp, --classpath Classpath to use. -cp, --classpath Classpath to use.
-m, --main <ns> Call the -main function from namespace with args. -m, --main <ns> Call the -main function from namespace with args.
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
--repl Start REPL --repl Start REPL
--socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --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. --time Print execution time before exiting.
@ -330,9 +333,13 @@ Everything after that is bound to *command-line-args*."))
file (try [(read-file file) nil] file (try [(read-file file) nil]
(catch Exception e (catch Exception e
(error-handler* e verbose?)))) (error-handler* e verbose?))))
expression (str (when preloads exit-code
(str preloads "\n")) ;; handle preloads
expression) (if exit-code exit-code
(do (when preloads (try (eval-string* sci-ctx preloads)
(catch Throwable e
(error-handler* e verbose?))))
nil))
exit-code exit-code
(or exit-code (or exit-code
(sci/with-bindings {reflection-var false} (sci/with-bindings {reflection-var false}
@ -346,7 +353,7 @@ Everything after that is bound to *command-line-args*."))
[(print-help) 0] [(print-help) 0]
repl [(repl/start-repl! sci-ctx) 0] repl [(repl/start-repl! sci-ctx) 0]
socket-repl [(start-socket-repl! socket-repl sci-ctx) 0] socket-repl [(start-socket-repl! socket-repl sci-ctx) 0]
expression (not (str/blank? expression))
(try (try
(loop [in (read-next *in*)] (loop [in (read-next *in*)]
(let [_ (swap! env update-in [:namespaces 'user] (let [_ (swap! env update-in [:namespaces 'user]
@ -371,6 +378,7 @@ Everything after that is bound to *command-line-args*."))
res))))) res)))))
(catch Throwable e (catch Throwable e
(error-handler* e verbose?))) (error-handler* e verbose?)))
uberscript [nil 0]
:else [(repl/start-repl! sci-ctx) 0])) :else [(repl/start-repl! sci-ctx) 0]))
1))) 1)))
t1 (System/currentTimeMillis)] t1 (System/currentTimeMillis)]
@ -381,8 +389,8 @@ Everything after that is bound to *command-line-args*."))
(spit uberscript-out "") ;; reset file (spit uberscript-out "") ;; reset file
(doseq [s @uberscript-sources] (doseq [s @uberscript-sources]
(spit uberscript-out s :append true)) (spit uberscript-out s :append true))
(spit uberscript-out expression :append true) (spit uberscript-out preloads :append true)
(spit uberscript-out file :append true))) (spit uberscript-out expression :append true)))
(when time? (binding [*out* *err*] (when time? (binding [*out* *err*]
(println "bb took" (str (- t1 t0) "ms.")))) (println "bb took" (str (- t1 t0) "ms."))))
exit-code)) exit-code))