[#403] --describe option

This commit is contained in:
Michiel Borkent 2020-04-30 23:06:56 +02:00
parent d65bb61d7c
commit 422baff333
3 changed files with 70 additions and 38 deletions

View file

@ -167,16 +167,17 @@ Check out the image on [Docker hub](https://hub.docker.com/r/borkdude/babashka/)
## Usage ## Usage
``` shellsession ``` shellsession
Usage: bb [ -i | -I ] [ -o | -O ] [ --stream ] [--verbose] Babashka v0.0.90
[ ( --classpath | -cp ) <cp> ] [ --uberscript <file> ]
[ ( --main | -m ) <main-namespace> | -e <expression> | -f <file> |
--repl | --socket-repl [<host>:]<port> | --nrepl-server [<host>:]<port> ]
[ arg* ]
Options: Options must appear in the order of groups mentioned below.
Help:
--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.
--describe Print an EDN map with information about this version of babashka.
In- and output flags:
-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.
@ -184,20 +185,25 @@ Options:
-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:
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file. --uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
Evaluation:
-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.
REPL:
--repl Start REPL. Use rlwrap for history. --repl Start REPL. Use rlwrap for history.
--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).
--nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667). --nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667).
--time Print execution time before exiting.
-- Stop parsing args and pass everything after -- to *command-line-args*
If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. Everything after that is bound to *command-line-args*. Use -- to separate script command lin args from bb command line args.
Everything after that is bound to *command-line-args*.
``` ```
The `clojure.core` functions are accessible without a namespace alias. The `clojure.core` functions are accessible without a namespace alias.

View file

@ -91,12 +91,12 @@
("--verbose")(recur (next options) ("--verbose")(recur (next options)
(assoc opts-map (assoc opts-map
:verbose? true)) :verbose? true))
("--describe") (recur (next options)
(assoc opts-map
:describe? true))
("--stream") (recur (next options) ("--stream") (recur (next options)
(assoc opts-map (assoc opts-map
:stream? true)) :stream? true))
("--time") (recur (next options)
(assoc opts-map
:time? true))
("-i") (recur (next options) ("-i") (recur (next options)
(assoc opts-map (assoc opts-map
:shell-in true)) :shell-in true))
@ -204,24 +204,20 @@
(defn print-version [] (defn print-version []
(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]
[ ( --classpath | -cp ) <cp> ] [ --uberscript <file> ]
[ ( --main | -m ) <main-namespace> | -e <expression> | -f <file> |
--repl | --socket-repl [<host>:]<port> | --nrepl-server [<host>:]<port> ]
[ arg* ]")
(defn print-usage []
(println usage-string))
(defn print-help [] (defn print-help []
(println (str "Babashka v" (str/trim (slurp (io/resource "BABASHKA_VERSION"))))) (println (str "Babashka v" (str/trim (slurp (io/resource "BABASHKA_VERSION")))))
;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION")))))
(println) (println)
(print-usage) (println "Options must appear in the order of groups mentioned below.")
(println)
(println "Options:")
(println " (println "
Help:
--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.
--describe Print an EDN map with information about this version of babashka.
In- and output flags:
-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.
@ -229,20 +225,50 @@
-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:
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file. --uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single executable file.
Evaluation:
-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.
REPL:
--repl Start REPL. Use rlwrap for history. --repl Start REPL. Use rlwrap for history.
--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).
--nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667). --nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667).
--time Print execution time before exiting.
-- Stop parsing args and pass everything after -- to *command-line-args*
If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. Everything after that is bound to *command-line-args*. Use -- to separate script command lin args from bb command line args."))
Everything after that is bound to *command-line-args*."))
(defn print-describe []
(println
(format
(str/trim "
{:babashka/version \"%s\"
:feature/core-async %s
:feature/csv %s
:feature/java-nio %s
:feature/java-time %s
:feature/xml %s
:feature/yaml %s
:feature/jdbc %s
:feature/postgresql %s
:feature/hsqldb %s}")
(str/trim (slurp (io/resource "BABASHKA_VERSION")))
features/core-async?
features/csv?
features/java-nio?
features/java-time?
features/xml?
features/yaml?
features/jdbc?
features/postgresql?
features/hsqldb?)))
(defn read-file [file] (defn read-file [file]
(let [f (io/file file)] (let [f (io/file file)]
@ -354,13 +380,12 @@ Everything after that is bound to *command-line-args*."))
(binding [*unrestricted* true] (binding [*unrestricted* true]
(sci/binding [reflection-var false (sci/binding [reflection-var false
sci/ns (vars/->SciNamespace 'user nil)] sci/ns (vars/->SciNamespace 'user nil)]
(let [t0 (System/currentTimeMillis) (let [{:keys [:version :shell-in :edn-in :shell-out :edn-out
{:keys [:version :shell-in :edn-in :shell-out :edn-out
:help? :file :command-line-args :help? :file :command-line-args
:expressions :stream? :time? :expressions :stream?
:repl :socket-repl :nrepl :repl :socket-repl :nrepl
:verbose? :classpath :verbose? :classpath
:main :uberscript] :as _opts} :main :uberscript :describe?] :as _opts}
(parse-opts args) (parse-opts args)
_ (when main (System/setProperty "babashka.main" main)) _ (when main (System/setProperty "babashka.main" main))
read-next (fn [*in*] read-next (fn [*in*]
@ -469,6 +494,8 @@ Everything after that is bound to *command-line-args*."))
[(print-version) 0] [(print-version) 0]
help? help?
[(print-help) 0] [(print-help) 0]
describe?
[(print-describe) 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]
nrepl [(start-nrepl! nrepl sci-ctx) 0] nrepl [(start-nrepl! nrepl sci-ctx) 0]
@ -497,8 +524,7 @@ Everything after that is bound to *command-line-args*."))
(error-handler* e verbose?))) (error-handler* e verbose?)))
uberscript [nil 0] uberscript [nil 0]
:else [(repl/start-repl! sci-ctx) 0])) :else [(repl/start-repl! sci-ctx) 0]))
1) 1)]
t1 (System/currentTimeMillis)]
(flush) (flush)
(when uberscript (when uberscript
uberscript uberscript
@ -508,8 +534,6 @@ Everything after that is bound to *command-line-args*."))
(spit uberscript-out s :append true)) (spit uberscript-out s :append true))
(spit uberscript-out preloads :append true) (spit uberscript-out preloads :append true)
(spit uberscript-out expression :append true))) (spit uberscript-out expression :append true)))
(when time? (binding [*out* *err*]
(println "bb took" (str (- t1 t0) "ms."))))
exit-code)))) exit-code))))
(defn -main (defn -main

View file

@ -37,7 +37,10 @@
(is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj")))
(is (thrown-with-msg? Exception #"does not exist" (bb nil "-help")))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))
(is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])"))) (is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])")))
(is (= '("-e" "1") (bb nil "-e" "*command-line-args*" "--" "-e" "1")))) (is (= '("-e" "1") (bb nil "-e" "*command-line-args*" "--" "-e" "1")))
(let [v (bb nil "--describe")]
(is (:babashka/version v))
(is (:feature/xml v))))
(deftest print-error-test (deftest print-error-test
(is (thrown-with-msg? Exception #"java.lang.NullPointerException" (is (thrown-with-msg? Exception #"java.lang.NullPointerException"
@ -111,9 +114,8 @@
exit-code (sci/with-bindings {sci/out out exit-code (sci/with-bindings {sci/out out
sci/err err} sci/err err}
(binding [*out* out *err* err] (binding [*out* out *err* err]
(main/main "--time" "(println \"Hello world!\") (System/exit 42)")))] (main/main "(println \"Hello world!\") (System/exit 42)")))]
(is (= (str out) "Hello world!\n")) (is (= (str out) "Hello world!\n"))
(is (re-find #"took" (str err)))
(is (= 42 exit-code)))) (is (= 42 exit-code))))
(deftest malformed-command-line-args-test (deftest malformed-command-line-args-test