add --println option

This commit is contained in:
Michiel Borkent 2019-08-09 23:59:19 +02:00
parent 97d3e34c91
commit 1c12c3bfe5
2 changed files with 28 additions and 9 deletions

View file

@ -48,12 +48,12 @@ You may also download a binary from [Github](https://github.com/borkdude/babashk
## Usage
``` shellsession
... | bb [--raw] '<Clojure form>'
... | bb [--raw] [--println] '<Clojure form>'
```
There is one special variable, `*in*`, which is the input read from stdin. When
the `--raw` flag is provided, `*in*` is a single string or vector of
strings. When it is omitted, the input is read as EDN.
There is one special variable, `*in*`, which is the input read from stdin. The
input is read as EDN by default, unless the `--raw` flag is provided. When using
the `--println` flag, the output is printed using `println` instead of `prn`.
The current version can be printed with:
@ -93,6 +93,22 @@ $ ls | bb --raw '(filterv #f(re-find #r "reflection" %) *in*)'
["reflection.json"]
```
Shuffle the lines of a file:
``` shellsession
$ cat /tmp/test.txt
1 Hello
2 Clojure
3 Babashka
4 Goodbye
$ cat /tmp/test.txt | bb --raw '(shuffle *in*)' | bb --println '(str/join "\n" *in*)'
3 Babashka
2 Clojure
4 Goodbye
1 Hello
```
Find the line numbers where the word Clojure occurs using a case insensitive regex:
``` shellsession

View file

@ -28,24 +28,27 @@
current-opt))
opts-map))
version (boolean (get opts "--version"))
raw (boolean (get opts "--raw"))]
raw (boolean (get opts "--raw"))
println? (boolean (get opts "--println"))]
{:version version
:raw raw}))
:raw raw
:println? println?}))
(defn -main
[& args]
(let [{:keys [:version :raw]} (parse-opts args)]
(let [{:keys [:version :raw :println?]} (parse-opts args)]
(cond version
(println (str/trim (slurp (io/resource "BABASHKA_VERSION"))))
:else
(let [expr (if raw (second args) (first args))
(let [expr (last args)
expr (read-edn expr)
in (slurp *in*)
in (if raw
(str/split in #"\n")
(read-edn (format "[%s]" in)))
in (if (= 1 (count in)) (first in) in)]
(prn (i/interpret expr in))))))
((if println? println prn)
(i/interpret expr in))))))
;;;; Scratch