From 1c12c3bfe55c5fc0c5125b79c285c21f494a082f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 9 Aug 2019 23:59:19 +0200 Subject: [PATCH] add --println option --- README.md | 24 ++++++++++++++++++++---- src/babashka/main.clj | 13 ++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index db5c17a2..72802a22 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,12 @@ You may also download a binary from [Github](https://github.com/borkdude/babashk ## Usage ``` shellsession -... | bb [--raw] '' +... | bb [--raw] [--println] '' ``` -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 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 5c258c61..21716c90 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -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