This commit is contained in:
Michiel Borkent 2021-03-20 16:44:44 +01:00
parent 3750ea0459
commit 7cc4867212
3 changed files with 47 additions and 16 deletions

View file

@ -81,13 +81,17 @@
(defn print-version [] (defn print-version []
(println (str "babashka v" version))) (println (str "babashka v" version)))
(def bb-edn
(atom nil))
(defn print-help [] (defn print-help [command-line-args]
(println (str "Babashka v" version)) (if (empty? command-line-args)
;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) (do
(println) (println (str "Babashka v" version))
(println "Options must appear in the order of groups mentioned below.") ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION")))))
(println " (println)
(println "Options must appear in the order of groups mentioned below.")
(println "
Help: Help:
--help, -h or -? Print this help text. --help, -h or -? Print this help text.
@ -132,7 +136,17 @@ If the first argument is not any of the above options, then it treated as a file
Everything after that is bound to *command-line-args*. Everything after that is bound to *command-line-args*.
Use -- to separate script command line args from bb command line args. Use -- to separate script command line args from bb command line args.
")) ")
[nil 0]) ;; end do
(let [k (first command-line-args)
k (keyword (subs k 1))
task (get-in @bb-edn [:tasks k])
help-text (:task/help task)]
(if help-text
[(println help-text) 0]
[(println "No help found for task:" k) 1])
,)) ;; end if
,) ;; end defn
(defn print-describe [] (defn print-describe []
(println (println
@ -362,9 +376,6 @@ Use -- to separate script command line args from bb command line args.
(println msg) (println msg)
{:exec (fn [] [nil exit])})) {:exec (fn [] [nil exit])}))
(def bb-edn
(atom nil))
(defn parse-opts [options] (defn parse-opts [options]
(let [fst (when options (first options)) (let [fst (when options (first options))
key? (when fst (str/starts-with? fst ":")) key? (when fst (str/starts-with? fst ":"))
@ -384,7 +395,8 @@ Use -- to separate script command line args from bb command line args.
("--clojure" ":clojure") (assoc opts-map :clojure true ("--clojure" ":clojure") (assoc opts-map :clojure true
:command-line-args (rest options)) :command-line-args (rest options))
("--version" ":version") {:version true} ("--version" ":version") {:version true}
("--help" "-h" "-?") {:help? true} ("--help" "-h" "-?" ":help") {:help true
:command-line-args (rest options)}
("--verbose")(recur (next options) ("--verbose")(recur (next options)
(assoc opts-map (assoc opts-map
:verbose? true)) :verbose? true))
@ -475,7 +487,7 @@ Use -- to separate script command line args from bb command line args.
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(update opts-map :expressions (fnil conj []) (first options)))) (update opts-map :expressions (fnil conj []) (first options))))
("--main", "-m") ("--main", "-m", ":main")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map :main (first options)))) (assoc opts-map :main (first options))))
@ -542,7 +554,7 @@ Use -- to separate script command line args from bb command line args.
sci/ns @sci/ns] sci/ns @sci/ns]
(let [{version-opt :version (let [{version-opt :version
:keys [:shell-in :edn-in :shell-out :edn-out :keys [:shell-in :edn-in :shell-out :edn-out
:help? :file :command-line-args :help :file :command-line-args
:expressions :stream? :expressions :stream?
:repl :socket-repl :nrepl :repl :socket-repl :nrepl
:verbose? :classpath :verbose? :classpath
@ -671,8 +683,8 @@ Use -- to separate script command line args from bb command line args.
(second (second
(cond version-opt (cond version-opt
[(print-version) 0] [(print-version) 0]
help? help
[(print-help) 0] (print-help command-line-args)
describe? describe?
[(print-describe) 0] [(print-describe) 0]
repl [(repl/start-repl! sci-ctx) 0] repl [(repl/start-repl! sci-ctx) 0]

View file

@ -42,6 +42,13 @@
:__ :print-hello]} :__ :print-hello]}
, ,
;;;; help
:cool-task {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]
:task/help "Usage: bb :cool-task
Sum up the numbers 1, 2 and 3."}
,
;;;; context (not implemented) ;;;; context (not implemented)
;; the problem with this is the DSL nature which is very opiniated and ;; the problem with this is the DSL nature which is very opiniated and
;; maybe not so nice to learn for people who only want to write Clojure ;; maybe not so nice to learn for people who only want to write Clojure

View file

@ -4,7 +4,8 @@
[babashka.fs :as fs] [babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.test :as test :refer [deftest is testing]])) [clojure.test :as test :refer [deftest is testing]]
[clojure.string :as str]))
(defn bb [& args] (defn bb [& args]
(edn/read-string (edn/read-string
@ -94,3 +95,14 @@
(with-config {:tasks {:describe {:task/type :babashka (with-config {:tasks {:describe {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]}}} :args ["-e" "(+ 1 2 3)"]}}}
(is (= 6 (bb :describe))))) (is (= 6 (bb :describe)))))
(deftest help-task-test
(with-config {:tasks {:cool-task {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]
:task/help "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element
0. ..."}}}
(is (str/includes? (apply test-utils/bb nil
(map str [:help :cool-task]))
"Usage: bb :cool-task"))))