Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active July 28, 2020 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/b5e14978572e4658bf74e9451ed5c322 to your computer and use it in GitHub Desktop.
Save borkdude/b5e14978572e4658bf74e9451ed5c322 to your computer and use it in GitHub Desktop.
Alternative to tools.cli in 10 lines of code adapted for babashka scripts that do not have a -main
#!/usr/bin/env bb
;; See https://vlaaad.github.io/tools-cli-in-10-lines-of-code
(ns other-ns)
(defn foo [& args]
(prn ::ns args))
(ns script)
(defn foo [& args]
(prn ::ns args))
(let [f #(try
(let [form (read-string %)]
(cond
(qualified-symbol? form) @(requiring-resolve form)
(symbol? form) @((ns-publics (symbol (namespace `-main))) form)
:else form))
(catch Exception _ %))
[f & args] (map f *command-line-args*)]
(some-> (apply f args) prn))
;; $ script.clj foo :x bar :y true
;; :script/ns (:x "bar" :y true)
;; $ script.clj other-ns/foo :x bar :y true
;; :other-ns/ns (:x "bar" :y true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment