Last active
July 28, 2020 10:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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