Skip to content

Instantly share code, notes, and snippets.

@adam-stokes
Created August 26, 2021 13:29
Show Gist options
  • Save adam-stokes/e601af3aeef2761cbfa25e0078ed8f9a to your computer and use it in GitHub Desktop.
Save adam-stokes/e601af3aeef2761cbfa25e0078ed8f9a to your computer and use it in GitHub Desktop.
Example bb.end with print usage

helper/cli.clj

(ns helper.cli
  (:require [clojure.edn :as edn]
            [clojure.string :as str]
            [babashka.fs :as fs]
            [babashka.tasks :refer [shell current-task run]]
            [lread.status-line :as status]))

(defn print-public-task [k]
  (let [{:keys [:private :name]} (current-task)]
    (when-not private
      (status/line :head (str (case k :enter "" "") " " name)))))

(defn print-help []
  "Prints the help output along with a usage if provided"
  (let [tasks (-> (or (System/getenv "BABASHKA_EDN") "bb.edn")
                  slurp
                  edn/read-string
                  :tasks)
        task (first *command-line-args*)]
    (if-let [task-map (get tasks (symbol task))]
      (println (format "%s\n\n\tUsage: bb %s%s\n"
                       (:doc task-map)
                       task
                       (if-let [usage (:usage task-map)]
                         (str " " usage)
                         "")))
      (do
        (println "Error: No such task exists")
        (System/exit 1)))))

bb.edn

{:min-bb-version "0.5.0"
 :paths ["syslib"]
 :deps {lread/status-line {:git/url "https://github.com/lread/status-line.git"
                           :sha "35ed39645038e81b42cb15ed6753b8462e60a06d"}}
 :tasks {
         :requires ([helper.cli :as cli])

         :enter (cli/print-public-task :enter)
         :leave (cli/print-public-task :leave)

         help
         {:doc "Print a task's help"
          :task (cli/print-help)}

         ;; life
         life:propane {:doc "Show wholesale propane prices"
                       :usage "bb life:propane --state NC"
                       :task (shell "bb bin/bb-propane-prices.clj --state NC")}
         }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment