This commit is contained in:
Michiel Borkent 2021-03-27 13:12:31 +01:00
parent 344ce2dca7
commit 235adfcfe5
2 changed files with 15 additions and 12 deletions

View file

@ -85,7 +85,10 @@
(atom nil)) (atom nil))
(defn decode-task [task] (defn decode-task [task]
(let [task-key (first task) (let [task (if (map? task)
(:task task)
task)
task-key (first task)
args (rest task) args (rest task)
maybe-opts (first args)] maybe-opts (first args)]
(if (map? maybe-opts) (if (map? maybe-opts)
@ -158,7 +161,7 @@ Use -- to separate script command line args from bb command line args.
(if-let [task (get-in @bb-edn [:tasks k])] (if-let [task (get-in @bb-edn [:tasks k])]
(let [{:keys [:args] (let [{:keys [:args]
task-key :task} (decode-task task)] task-key :task} (decode-task task)]
(if-let [help-text (:help (meta task))] (if-let [help-text (:help task)]
[(println help-text) 0] [(println help-text) 0]
(if-let [main (when (= :main task-key) (if-let [main (when (= :main task-key)
(first args))] (first args))]
@ -184,7 +187,7 @@ Use -- to separate script command line args from bb command line args.
(println) (println)
(doseq [[k v] tasks] (doseq [[k v] tasks]
(println (str (format fmt k) (println (str (format fmt k)
(when-let [d (:description (meta v))] (when-let [d (:description v)]
(str " " d))))) (str " " d)))))
(println) (println)
(println "Run bb :help <task> to view help of a specific task.") (println "Run bb :help <task> to view help of a specific task.")

View file

@ -106,30 +106,30 @@
(deftest help-task-test (deftest help-task-test
(with-config {:tasks {:cool-task (with-config {:tasks {:cool-task
^{:help "Usage: bb :cool-task {:help "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..."} 0. ..."
[:babashka "-e" "(+ 1 2 3)"]}} :task [:babashka "-e" "(+ 1 2 3)"]}}}
(is (str/includes? (apply test-utils/bb nil (is (str/includes? (apply test-utils/bb nil
(map str [:help :cool-task])) (map str [:help :cool-task]))
"Usage: bb :cool-task")))) "Usage: bb :cool-task"))))
(deftest list-tasks-test (deftest list-tasks-test
(with-config {:tasks {:task-1 (with-config {:tasks {:task-1
^{:description "Return the sum of 1, 2 and 3." {:description "Return the sum of 1, 2 and 3."
:help "Usage: bb :cool-task :help "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..."} 0. ..."}
[:babashka "-e" "(+ 1 2 3)"] :task [:babashka "-e" "(+ 1 2 3)"]
:cool-task-2 :cool-task-2
^{:description "Return the sum of 4, 5 and 6." {:description "Return the sum of 4, 5 and 6."
:help "Usage: bb :cool-task :help "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..."} 0. ..."
[:babashka "-e" "(+ 4 5 6)"]}} :task [:babashka "-e" "(+ 4 5 6)"]}}}
(let [res (apply test-utils/bb nil (let [res (apply test-utils/bb nil
(map str [:tasks]))] (map str [:tasks]))]
(is (str/includes? res "The following tasks are available:")) (is (str/includes? res "The following tasks are available:"))