From 94c7893dd0a17d7b42d5e48ad543fed2bf8e6192 Mon Sep 17 00:00:00 2001 From: sohalt Date: Wed, 13 Mar 2024 14:48:33 +0100 Subject: [PATCH 1/3] Refactor --- src/babashka/cli.cljc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index d907bc3..d9f6dcb 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -653,20 +653,18 @@ ([tree args] (dispatch-tree tree args nil)) ([tree args opts] - (let [{:as res :keys [cmd-info error wrong-input available-commands]} + (let [{:as res :keys [cmd-info error available-commands]} (dispatch-tree' tree args opts) - error-fn* (or (:error-fn opts) + error-fn (or (:error-fn opts) (fn [{:keys [msg] :as data}] - (throw (ex-info msg data)))) - error-fn (fn [data] - (-> {;; :tree tree - :type :org.babashka/cli - :wrong-input wrong-input :all-commands available-commands} - (merge data) - error-fn*))] + (throw (ex-info msg data))))] (case error (:no-match :input-exhausted) - (error-fn {:cause error :opts (:opts res)}) + (error-fn (merge + {:type :org.babashka/cli + :cause error + :all-commands available-commands} + (select-keys res [:wrong-input :opts]))) nil ((:fn cmd-info) (dissoc res :cmd-info)))))) (defn dispatch From 20b1414c7c3673f9bc51f88bb3f9492654a27584 Mon Sep 17 00:00:00 2001 From: sohalt Date: Wed, 13 Mar 2024 14:49:05 +0100 Subject: [PATCH 2/3] Expose matched cmds as :dispatch in :no-match and :input-exhausted errors --- src/babashka/cli.cljc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index d9f6dcb..79af98d 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -644,9 +644,11 @@ {:error :no-match :wrong-input arg :available-commands (keys (:cmd cmd-info)) + :dispatch cmds :opts (dissoc all-opts ::opts-by-cmds)} {:error :input-exhausted :available-commands (keys (:cmd cmd-info)) + :dispatch cmds :opts (dissoc all-opts ::opts-by-cmds)}))))))) (defn- dispatch-tree @@ -664,7 +666,7 @@ {:type :org.babashka/cli :cause error :all-commands available-commands} - (select-keys res [:wrong-input :opts]))) + (select-keys res [:wrong-input :opts :dispatch]))) nil ((:fn cmd-info) (dissoc res :cmd-info)))))) (defn dispatch From 2b5ae090ecf94c97384956f244021a4e8cb1a788 Mon Sep 17 00:00:00 2001 From: sohalt Date: Fri, 15 Mar 2024 08:10:06 +0100 Subject: [PATCH 3/3] Add test --- test/babashka/cli_test.cljc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/babashka/cli_test.cljc b/test/babashka/cli_test.cljc index bf1459f..aa39e8c 100644 --- a/test/babashka/cli_test.cljc +++ b/test/babashka/cli_test.cljc @@ -408,7 +408,12 @@ (is (= {:dispatch ["foo" "bar"], :opts {:version "2000"}, :args ["some-arg"]} (-> (cli/dispatch table - ["foo" "bar" "--version" "2000" "some-arg"])))))))) + ["foo" "bar" "--version" "2000" "some-arg"]))))) + (testing "dispatch errors return :dispatch key" + (is (= {:type :org.babashka/cli, :dispatch ["foo" "bar"], :all-commands '("baz"), :cause :input-exhausted, :opts {}} + (cli/dispatch [{:cmds ["foo" "bar" "baz"] :fn identity}] ["foo" "bar"] {:error-fn identity}))) + (is (= {:type :org.babashka/cli, :dispatch ["foo" "bar"], :wrong-input "wrong", :all-commands '("baz"), :cause :no-match, :opts {}} + (cli/dispatch [{:cmds ["foo" "bar" "baz"] :fn identity}] ["foo" "bar" "wrong"] {:error-fn identity}))))))) (deftest table->tree-test (testing "internal represenation"