diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 88a9a97b..fda84fbb 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -81,13 +81,17 @@ (defn print-version [] (println (str "babashka v" version))) +(def bb-edn + (atom nil)) -(defn print-help [] - (println (str "Babashka v" version)) - ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) - (println) - (println "Options must appear in the order of groups mentioned below.") - (println " +(defn print-help [command-line-args] + (if (empty? command-line-args) + (do + (println (str "Babashka v" version)) + ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) + (println) + (println "Options must appear in the order of groups mentioned below.") + (println " Help: --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*. 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 [] (println @@ -362,9 +376,6 @@ Use -- to separate script command line args from bb command line args. (println msg) {:exec (fn [] [nil exit])})) -(def bb-edn - (atom nil)) - (defn parse-opts [options] (let [fst (when options (first options)) 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 :command-line-args (rest options)) ("--version" ":version") {:version true} - ("--help" "-h" "-?") {:help? true} + ("--help" "-h" "-?" ":help") {:help true + :command-line-args (rest options)} ("--verbose")(recur (next options) (assoc opts-map :verbose? true)) @@ -475,7 +487,7 @@ Use -- to separate script command line args from bb command line args. (let [options (next options)] (recur (next options) (update opts-map :expressions (fnil conj []) (first options)))) - ("--main", "-m") + ("--main", "-m", ":main") (let [options (next options)] (recur (next 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] (let [{version-opt :version :keys [:shell-in :edn-in :shell-out :edn-out - :help? :file :command-line-args + :help :file :command-line-args :expressions :stream? :repl :socket-repl :nrepl :verbose? :classpath @@ -671,8 +683,8 @@ Use -- to separate script command line args from bb command line args. (second (cond version-opt [(print-version) 0] - help? - [(print-help) 0] + help + (print-help command-line-args) describe? [(print-describe) 0] repl [(repl/start-repl! sci-ctx) 0] diff --git a/test-resources/bb.edn b/test-resources/bb.edn index 95257e3c..727f4c9b 100644 --- a/test-resources/bb.edn +++ b/test-resources/bb.edn @@ -42,6 +42,13 @@ :__ :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) ;; 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 diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 389745a1..f7d9ec38 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -4,7 +4,8 @@ [babashka.fs :as fs] [babashka.test-utils :as test-utils] [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] (edn/read-string @@ -94,3 +95,14 @@ (with-config {:tasks {:describe {:task/type :babashka :args ["-e" "(+ 1 2 3)"]}}} (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"))))