Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active August 3, 2022 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/29926d5ece9f79b04629a822fdc4e011 to your computer and use it in GitHub Desktop.
Save borkdude/29926d5ece9f79b04629a822fdc4e011 to your computer and use it in GitHub Desktop.
babashka.cli args->opts
(ns cli-app
(:require [babashka.cli :as cli]))
(def spec {:hello {:desc "something"}
:foo {:desc "foo"}
:bar {:desc "bar"
:coerce []}})
(def cli-opts {:spec spec
:args->opts (cons :foo (repeat :bar))})
(defn foo
{:org.babashka/cli cli-opts}
[m]
(if (:help m)
(do
(println "Usage: foo [OPTIONS] FOO BAR+")
(println)
(println "Options:")
(println (cli/format-opts {:spec spec}))
(println))
m))
(foo (cli/parse-opts *command-line-args* cli-opts))
;; $ bb /tmp/cli_app.clj --hello there arg1 arg2 arg3
;; {:hello "there", :foo "arg1", :bar ["arg2" "arg3"]}
;; $ bb /tmp/cli_app.clj --help
;; Usage: foo [OPTIONS] FOO BAR+
;; Options:
;; --hello something
;; --foo foo
;; --bar bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment