Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #80: add :opts to :error-fn #81

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ For breaking changes, check [here](#breaking-changes).

[Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs!

## Unreleased
## v0.8.56 (2024-02-13)

- Add `:opts` to `:error-fn` input
- Fix command line args for test runner `--dirs`, `--only`, etc

## v0.8.55 (2024-01-04)
Expand Down
12 changes: 8 additions & 4 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@
:msg #?(:clj (.getMessage e)
:cljs (ex-message e))
:option current-opt
:value arg})
:value arg
:opts acc})
;; Since we've encountered an error, don't add this opt
acc))
opt
Expand All @@ -462,14 +463,16 @@
(error-fn {:cause :restrict
:msg (str "Unknown option: " k)
:restrict restrict
:option k}))))
:option k
:opts opts}))))
(when require
(doseq [k require]
(when-not (find opts k)
(error-fn {:cause :require
:msg (str "Required option: " k)
:require require
:option k}))))
:option k
:opts opts}))))
(when validate
(doseq [[k vf] validate]
(let [f (or (and
Expand All @@ -487,7 +490,8 @@
:msg (ex-msg-fn {:option k :value v})
:validate validate
:option k
:value v})))))))
:value v
:opts opts})))))))
opts)))

(defn parse-args
Expand Down
46 changes: 16 additions & 30 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
:msg "Coerce failure: cannot transform input \"dude\" to long"
:option :b
:value "dude"
:spec nil}
:spec nil
:opts {}}
(ex-data e)))))
(is (submap? {:a [1 1]}
(cli/parse-opts ["-a" "1" "-a" "1"] {:collect {:a []} :coerce {:a :long}})))
Expand Down Expand Up @@ -123,7 +124,8 @@
:msg "Unknown option: :b"
:option :b
:restrict #{:foo}
:spec {:foo {}}}
:spec {:foo {}}
:opts {:foo "bar", :b true}}
(ex-data e))))))
(testing ":closed #{:foo} w/ only --foo in opts is allowed"
(is (= {:foo "bar"} (cli/parse-opts ["--foo=bar"]
Expand All @@ -142,7 +144,8 @@
:msg "Unknown option: :bar"
:option :bar
:restrict #{:foo}
:spec nil}
:spec nil
:opts {:foo true, :bar true}}
(ex-data e))))))
(testing ":closed true w/ :aliases {:f :foo} w/ only -f in opts is allowed"
(is (= {:foo true} (cli/parse-opts ["-f"]
Expand Down Expand Up @@ -508,11 +511,12 @@
:spec nil
:value 0
:validate {:foo {:pred pos?
:ex-msg ex-msg-fn}}}
:ex-msg ex-msg-fn}}
:opts {:foo 0}}
(ex-data e)))))))

(deftest error-fn-test
(let [errors (atom #{})
(let [errors (atom [])
spec {:a {:require true}
:b {:validate pos?}
:c {:coerce :long}}]
Expand All @@ -522,31 +526,13 @@
{:error-fn (fn [error] (swap! errors conj error))
:restrict true
:spec spec})
(is (= #{{:type :org.babashka/cli
:cause :validate
:msg "Invalid value for option :b: 0"
:option :b
:value 0
:validate {:b pos?}
:spec spec}
{:type :org.babashka/cli
:cause :coerce
:msg "Coerce failure: cannot transform input \"nope!\" to long"
:option :c
:value "nope!"
:spec spec}
{:type :org.babashka/cli
:cause :restrict
:msg "Unknown option: :extra"
:option :extra
:restrict #{:a :b :c}
:spec spec}
{:type :org.babashka/cli
:cause :require
:msg (str "Required option: :a")
:require #{:a}
:option :a
:spec spec}}
(is (= [{:spec spec, :type :org.babashka/cli, :cause :coerce,
:msg "Coerce failure: cannot transform input \"nope!\" to long", :option :c,
:value "nope!", :opts {:b 0}}
{:spec spec, :type :org.babashka/cli, :cause :restrict, :msg "Unknown option: :extra", :restrict #{:c :b :a}, :option :extra, :opts {:b 0, :extra "bad!"}}
{:spec spec, :type :org.babashka/cli, :cause :require, :msg "Required option: :a", :require #{:a}, :option :a, :opts {:b 0, :extra "bad!"}}
{:spec spec, :type :org.babashka/cli, :cause :validate, :msg "Invalid value for option :b: 0", :validate {:b pos?}, :option :b, :value 0,
:opts {:b 0, :extra "bad!"}}]
@errors))))

(deftest exec-args-replaced-test
Expand Down
Loading