tools.cli (#68)

* tools.cli

* migrate to new namespace model
This commit is contained in:
Michiel Borkent 2019-09-08 23:07:58 +02:00 committed by GitHub
parent 4d0b9aef42
commit 697360a265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 125 additions and 101 deletions

View file

@ -136,6 +136,7 @@ explicitly.
it is a function. it is a function.
- [`me.raynes.conch.low-level`](https://github.com/clj-commons/conch#low-level-usage) - [`me.raynes.conch.low-level`](https://github.com/clj-commons/conch#low-level-usage)
aliased as `conch` aliased as `conch`
- [`clojure.tools.cli`](https://github.com/clojure/tools.cli) aliased as `tools.cli`
From Java the following is available: From Java the following is available:

View file

@ -10,7 +10,8 @@
:source-paths ["src" "sci/src" "sci/inlined"] :source-paths ["src" "sci/src" "sci/inlined"]
:resource-paths ["resources" "sci/resources"] :resource-paths ["resources" "sci/resources"]
:dependencies [[org.clojure/clojure "1.10.1"] :dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/core.async "0.4.500"]] [org.clojure/core.async "0.4.500"]
[org.clojure/tools.cli "0.4.2"]]
:profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]} :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]}
:uberjar {:global-vars {*assert* false} :uberjar {:global-vars {*assert* false}
:jvm-opts ["-Dclojure.compiler.direct-linking=true" :jvm-opts ["-Dclojure.compiler.direct-linking=true"

2
sci

@ -1 +1 @@
Subproject commit 7343d3912c4a486211200f2e81297ad4ef525647 Subproject commit d4a893fa5d3a48de9779ac52e1ea8f927278e8d2

View file

@ -6,63 +6,63 @@
[& body] [& body]
`(~'async/thread-call (fn [] ~@body))) `(~'async/thread-call (fn [] ~@body)))
(def async-bindings (def async-namespace
{'async/<!! async/<!! {'<!! async/<!!
'async/>!! async/>!! '>!! async/>!!
'async/admix async/admix 'admix async/admix
'async/alts! async/alts! 'alts! async/alts!
'async/alts!! async/alts!! 'alts!! async/alts!!
'async/buffer async/buffer 'buffer async/buffer
'async/chan async/chan 'chan async/chan
'async/close! async/close! 'close! async/close!
'async/do-alt async/do-alt 'do-alt async/do-alt
'async/do-alts async/do-alts 'do-alts async/do-alts
'async/dropping-buffer async/dropping-buffer 'dropping-buffer async/dropping-buffer
'async/filter< async/filter< 'filter< async/filter<
'async/filter> async/filter> 'filter> async/filter>
'async/into async/into 'into async/into
'async/map async/map 'map async/map
'async/map< async/map< 'map< async/map<
'async/map> async/map> 'map> async/map>
'async/mapcat< async/mapcat< 'mapcat< async/mapcat<
'async/mapcat> async/mapcat> 'mapcat> async/mapcat>
'async/merge async/merge 'merge async/merge
'async/mix async/mix 'mix async/mix
'async/mult async/mult 'mult async/mult
'async/offer! async/offer! 'offer! async/offer!
'async/onto-chan async/onto-chan 'onto-chan async/onto-chan
'async/partition async/partition 'partition async/partition
'async/partition-by async/partition-by 'partition-by async/partition-by
'async/pipe async/pipe 'pipe async/pipe
'async/pipeline async/pipeline 'pipeline async/pipeline
'async/pipeline-async async/pipeline-async 'pipeline-async async/pipeline-async
'async/pipeline-blocking async/pipeline-blocking 'pipeline-blocking async/pipeline-blocking
'async/poll! async/poll! 'poll! async/poll!
'async/promise-chan async/promise-chan 'promise-chan async/promise-chan
'async/pub async/pub 'pub async/pub
'async/put! async/put! 'put! async/put!
'async/reduce async/reduce 'reduce async/reduce
'async/remove< async/remove< 'remove< async/remove<
'async/remove> async/remove> 'remove> async/remove>
'async/sliding-buffer async/sliding-buffer 'sliding-buffer async/sliding-buffer
'async/solo-mode async/solo-mode 'solo-mode async/solo-mode
'async/split async/split 'split async/split
'async/sub async/sub 'sub async/sub
'async/take async/take 'take async/take
'async/take! async/take! 'take! async/take!
'async/tap async/tap 'tap async/tap
'async/thread (with-meta thread {:sci/macro true}) 'thread (with-meta thread {:sci/macro true})
'async/thread-call async/thread-call 'thread-call async/thread-call
'async/timeout async/timeout 'timeout async/timeout
'async/to-chan async/to-chan 'to-chan async/to-chan
'async/toggle async/toggle 'toggle async/toggle
'async/transduce async/transduce 'transduce async/transduce
'async/unblocking-buffer? async/unblocking-buffer? 'unblocking-buffer? async/unblocking-buffer?
'async/unique async/unique 'unique async/unique
'async/unmix async/unmix 'unmix async/unmix
'async/unmix-all async/unmix-all 'unmix-all async/unmix-all
'async/unsub async/unsub 'unsub async/unsub
'async/unsub-all async/unsub-all 'unsub-all async/unsub-all
'async/untap async/untap 'untap async/untap
'async/untap-all async/untap-all}) 'untap-all async/untap-all})

View file

@ -7,8 +7,7 @@
`(~'future-call (fn [] ~@body))) `(~'future-call (fn [] ~@body)))
(def core-bindings (def core-bindings
{;; atoms {'atom atom
'atom atom
'swap! swap! 'swap! swap!
'swap-vals! swap-vals! 'swap-vals! swap-vals!
'reset! reset! 'reset! reset!
@ -41,4 +40,5 @@
'flush flush 'flush flush
'ex-info ex-info 'ex-info ex-info
'ex-data ex-data 'ex-data ex-data
'read-line read-line}) 'read-line read-line
'namespace namespace})

View file

@ -1,13 +1,13 @@
(ns babashka.impl.clojure.java.io (ns babashka.impl.clojure.java.io
(:require [clojure.java.io :as io])) (:require [clojure.java.io :as io]))
(def io-bindings (def io-namespace
{'io/as-relative-path io/as-relative-path {'as-relative-path io/as-relative-path
'io/copy io/copy 'copy io/copy
'io/delete-file io/delete-file 'delete-file io/delete-file
'io/file io/file 'file io/file
'io/input-stream io/input-stream 'input-stream io/input-stream
'io/make-parents io/make-parents 'make-parents io/make-parents
'io/output-stream io/output-stream 'output-stream io/output-stream
'io/reader io/reader 'reader io/reader
'io/writer io/writer}) 'writer io/writer})

View file

@ -3,16 +3,16 @@
(:require (:require
[babashka.impl.me.raynes.conch.low-level :as ll])) [babashka.impl.me.raynes.conch.low-level :as ll]))
(def conch-bindings (def conch-namespace
{;; low level API {;; low level API
'conch/proc ll/proc 'proc ll/proc
'conch/destroy ll/destroy 'destroy ll/destroy
'conch/exit-code ll/exit-code 'exit-code ll/exit-code
'conch/flush ll/flush 'flush ll/flush
'conch/done ll/done 'done ll/done
'conch/stream-to ll/stream-to 'stream-to ll/stream-to
'conch/feed-from ll/feed-from 'feed-from ll/feed-from
'conch/stream-to-string ll/stream-to-string 'stream-to-string ll/stream-to-string
'conch/stream-to-out ll/stream-to-out 'stream-to-out ll/stream-to-out
'conch/feed-from-string ll/feed-from-string 'feed-from-string ll/feed-from-string
'conch/read-line ll/read-line}) 'read-line ll/read-line})

View file

@ -0,0 +1,9 @@
(ns babashka.impl.tools.cli
{:no-doc true}
(:require [clojure.tools.cli :as tools.cli]))
(def tools-cli-namespace
{'format-lines tools.cli/format-lines
'summarize tools.cli/summarize
'get-default-options tools.cli/get-default-options
'parse-opts tools.cli/parse-opts})

View file

@ -4,13 +4,14 @@
[babashka.impl.File :refer [file-bindings]] [babashka.impl.File :refer [file-bindings]]
[babashka.impl.System :refer [system-bindings]] [babashka.impl.System :refer [system-bindings]]
[babashka.impl.Thread :refer [thread-bindings]] [babashka.impl.Thread :refer [thread-bindings]]
[babashka.impl.async :refer [async-bindings]] [babashka.impl.async :refer [async-namespace]]
[babashka.impl.clojure.core :refer [core-bindings]] [babashka.impl.clojure.core :refer [core-bindings]]
[babashka.impl.clojure.java.io :refer [io-bindings]] [babashka.impl.clojure.java.io :refer [io-namespace]]
[babashka.impl.clojure.stacktrace :refer [print-stack-trace]] [babashka.impl.clojure.stacktrace :refer [print-stack-trace]]
[babashka.impl.conch :refer [conch-bindings]] [babashka.impl.conch :refer [conch-namespace]]
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]] [babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
[babashka.impl.socket-repl :as socket-repl] [babashka.impl.socket-repl :as socket-repl]
[babashka.impl.tools.cli :refer [tools-cli-namespace]]
[babashka.net :as net] [babashka.net :as net]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.java.io :as io] [clojure.java.io :as io]
@ -140,19 +141,10 @@ Everything after that is bound to *command-line-args*."))
(throw (Exception. (str "File does not exist: " file)))))) (throw (Exception. (str "File does not exist: " file))))))
(def bindings (def bindings
(merge {'shell/sh shell/sh (merge core-bindings
'namespace namespace
;; clojure.java.io
'edn/read-string edn/read-string
'net/wait-for-it net/wait-for-it
'sig/pipe-signal-received? pipe-signal-received?}
core-bindings
io-bindings
system-bindings system-bindings
file-bindings file-bindings
thread-bindings thread-bindings))
conch-bindings
async-bindings))
(defn read-edn [] (defn read-edn []
(edn/read {;;:readers *data-readers* (edn/read {;;:readers *data-readers*
@ -194,7 +186,23 @@ Everything after that is bound to *command-line-args*."))
:else :else
(edn/read *in*)))))) (edn/read *in*))))))
env (atom {}) env (atom {})
ctx {:bindings (assoc bindings '*command-line-args* command-line-args) ctx {:aliases '{tools.cli 'clojure.tools.cli
edn clojure.edn
net babashka.net
sig babashka.signal
shell clojure.java.shell
io clojure.java.io
conch me.raynes.conch.low-level
async clojure.core.async}
:namespaces {'clojure.tools.cli tools-cli-namespace
'clojure.edn {'read-string edn/read-string}
'clojure.java.shell {'sh shell/sh}
'babashka.net {'wait-for-it net/wait-for-it}
'babashka.signal {'pipe-signal-received? pipe-signal-received?}
'clojure.java.io io-namespace
'me.raynes.conch.low-level conch-namespace
'clojure.core.async async-namespace}
:bindings (assoc bindings '*command-line-args* command-line-args)
:env env} :env env}
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %)) ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))
_preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx)) _preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx))

View file

@ -185,3 +185,6 @@
(-> (async/alts!! [(async-command \"sleep 2 && echo process 1\") (-> (async/alts!! [(async-command \"sleep 2 && echo process 1\")
(async-command \"sleep 1 && echo process 2\")]) (async-command \"sleep 1 && echo process 2\")])
first :out str/trim println)")))) first :out str/trim println)"))))
(deftest tools-cli-test
(is (= {:file "README.md"} (bb nil "test/babashka/scripts/tools.cli.bb"))))

View file

@ -0,0 +1,2 @@
(require '[clojure.tools.cli :refer [parse-opts]])
(:options (parse-opts ["-f" "README.md"] [["-f" "--file FILE" "file"]]))