From 82e1a7e5377f94f0d93810fa64538a4ce009d33a Mon Sep 17 00:00:00 2001 From: Pavel Shkadzko Date: Mon, 26 Feb 2024 10:25:44 +0100 Subject: [PATCH 1/3] add simple spec example --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index 297f512..3a1f239 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ your exec functions into CLIs. ## TOC +- [Simple example](#simple-example) - [Options](#options) - [Arguments](#arguments) - [Subcommands](#subcommands) @@ -71,6 +72,72 @@ your exec functions into CLIs. - [Clojure CLI](#clojure-cli) - [Leiningen](#leiningen) +## Simple example +Here is an example script to get you started! + +```clojure +#!/usr/bin/env bb +(require '[babashka.cli :as cli] + '[babashka.fs :as fs]) + +(defn dir-exists? + [path] + (fs/directory? path)) + +(defn show-help + [spec] + (cli/format-opts (merge spec {:order (vec (keys (:spec spec)))}))) + +(def cli-spec + {:spec + {:num {:coerce :long + :desc "Number of some items" + :alias :n ; adds -n alias for --num + :validate pos? ; tests if supplied --num >0 + :require true} ; --num,-n is required + :dir {:desc "Directory name to do stuff" + :alias :d + :validate dir-exists?} ; tests if --dir exists + :flag {:coerce :boolean ; defines a boolean flag + :desc "I am just a flag"}} + :error-fn ; a function to handle errors + (fn [{:keys [spec type cause msg option] :as data}] + (if (= :org.babashka/cli type) + (case cause + :require + (println + (format "Missing required argument: %s\n" option)) + :validate + (println + (format "%s does not exist!\n" msg))))) + }) + +(defn -main + [args] + (let [opts (cli/parse-opts args cli-spec)] + (if (or (:help opts) (:h opts)) + (println (show-help cli-spec)) + (println "Here are your cli args!:" opts)))) + +(-main *command-line-args*) +``` + +And this is how you run it: +``` +$ bb try-me.clj --num 1 --dir my_dir --flag +Here are your cli args!: {:num 1, :dir my_dir, :flag true} + +$ bb try-me.clj --help +Missing required argument: :num + + -n, --num Number of some items + -d, --dir Directory name to do stuff + --flag I am just a flag +``` + +Using the above [`spec`](https://github.com/babashka/cli#spec) format is optional and you can implement you own parsing logic just with [`parse-opts`/`parse-args`](https://github.com/babashka/cli#options). +However, many would find the above example familiar. + ## Options For parsing options, use either [`parse-opts`](https://github.com/babashka/cli/blob/main/API.md#parse-opts) or [`parse-args`](https://github.com/babashka/cli/blob/main/API.md#parse-args). From 5455e53a4447f835cd88d884d74eb303cd741f75 Mon Sep 17 00:00:00 2001 From: Pavel Shkadzko Date: Mon, 26 Feb 2024 10:29:44 +0100 Subject: [PATCH 2/3] updated example comments identation --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3a1f239..4d0e50e 100644 --- a/README.md +++ b/README.md @@ -92,15 +92,15 @@ Here is an example script to get you started! {:spec {:num {:coerce :long :desc "Number of some items" - :alias :n ; adds -n alias for --num - :validate pos? ; tests if supplied --num >0 - :require true} ; --num,-n is required + :alias :n ; adds -n alias for --num + :validate pos? ; tests if supplied --num >0 + :require true} ; --num,-n is required :dir {:desc "Directory name to do stuff" :alias :d - :validate dir-exists?} ; tests if --dir exists - :flag {:coerce :boolean ; defines a boolean flag + :validate dir-exists?} ; tests if --dir exists + :flag {:coerce :boolean ; defines a boolean flag :desc "I am just a flag"}} - :error-fn ; a function to handle errors + :error-fn ; a function to handle errors (fn [{:keys [spec type cause msg option] :as data}] (if (= :org.babashka/cli type) (case cause From 69cd4ab9f66ca21badc9d76508954b5ee973cb73 Mon Sep 17 00:00:00 2001 From: Pavel Shkadzko Date: Mon, 26 Feb 2024 10:30:59 +0100 Subject: [PATCH 3/3] removed "above" dup --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d0e50e..6813568 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Missing required argument: :num --flag I am just a flag ``` -Using the above [`spec`](https://github.com/babashka/cli#spec) format is optional and you can implement you own parsing logic just with [`parse-opts`/`parse-args`](https://github.com/babashka/cli#options). +Using the [`spec`](https://github.com/babashka/cli#spec) format is optional and you can implement you own parsing logic just with [`parse-opts`/`parse-args`](https://github.com/babashka/cli#options). However, many would find the above example familiar. ## Options