Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 19, 2023
1 parent e331bcf commit 2dad2e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
36 changes: 22 additions & 14 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
(when (= prefix a)
suffix)))

(defn table->tree [table]
(defn- table->tree [table]
(reduce (fn [tree {:as cfg :keys [cmds]}]
(assoc-in tree cmds (dissoc cfg :cmds)))
{} table))
Expand Down Expand Up @@ -615,21 +615,29 @@
(dispatch-tree tree args nil))
([tree args opts]
(let [{:as res :keys [cmd-info error wrong-input available-commands]}
(dispatch-tree' tree args opts)]
(dispatch-tree' tree args 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*))]
(case error
;; TODO: decide to print or return this via :error-fn or so?
;; Either way, we need to print to stderr
(:no-match :input-exhausted)
(let [println (fn [& args]
#?(:cljs (doseq [a args]
(*print-err-fn* a)
(*print-err-fn* "\n"))
:clj (binding [*out* *err*]
(apply println args))))]
(println (str "No matching command" (when wrong-input
(str ": " wrong-input))))
(println "Available commands:")
(println (str/join "\n" available-commands)))
(error-fn {:cause error})
(when-let [f (:error-fn opts)]
(let [println (fn [& args]
#?(:cljs (doseq [a args]
(*print-err-fn* a)
(*print-err-fn* "\n"))
:clj (binding [*out* *err*]
(apply println args))))]
(println (str "No matching command" (when wrong-input
(str ": " wrong-input))))
(println "Available commands:")
(println (str/join "\n" available-commands))))
nil ((:fn cmd-info) (dissoc res :cmd-info))))))

(defn dispatch
Expand Down
36 changes: 16 additions & 20 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@
:fn identity,
"baz" {:spec {:quux {:coerce :keyword}},
:fn identity}}}}
(cli/table->tree [{:cmds ["foo" "bar"]
:spec {:baz {:coerce :boolean}}
:fn identity}
{:cmds ["foo" "bar" "baz"]
:spec {:quux {:coerce :keyword}}
:fn identity}]))))
(#'cli/table->tree [{:cmds ["foo" "bar"]
:spec {:baz {:coerce :boolean}}
:fn identity}
{:cmds ["foo" "bar" "baz"]
:spec {:quux {:coerce :keyword}}
:fn identity}]))))

(deftest dispatch-tree-test
(d/deflet
Expand All @@ -304,16 +304,13 @@
{:cmds ["foo" "bar" "baz"]
:spec {:quux {:coerce :keyword}}
:fn identity}])
(is (= (str/split-lines "No matching command\nAvailable commands:\nfoo\n")
(str/split-lines (with-out-str
(binding #?(:clj [*err* *out*]
:cljs [*print-err-fn* *print-fn*
*print-newline* true])
(cli/dispatch table []))))))
#_#_(is (= (str/split-lines "No matching command\nAvailable commands:\nbar\n")
(str/split-lines (with-out-str (cli/dispatch table ["foo" "--baz" "quux"])))))
(is (= (str/split-lines "No matching command: baz\nAvailable commands:\nbar\n")
(str/split-lines (with-out-str (cli/dispatch table ["foo" "baz" "--baz" "quux"])))))
(def tree (#'cli/table->tree table))
(is (submap? {:tree tree
:type :org.babashka/cli
:cause :input-exhausted
:all-commands ["foo"]}
(try (cli/dispatch table [])
(catch Exception e (ex-data e)))))
(is (= {:dispatch ["foo" "bar"], :opts {:baz true}, :args ["quux"]}
(cli/dispatch table ["foo" "bar" "--baz" "quux"])))
(is (= {:dispatch ["foo" "bar" "baz"] , :opts {:baz true :quux :xyzzy}, :args nil}
Expand All @@ -324,12 +321,11 @@
"baz" {:fn identity}}
:spec {:bar {:coerce :keyword}}
:fn identity}})
;; TODO: change :dispatch to :cmds?
(is (submap?
{:dispatch ["foo" "bar"]
:opts {:bar :bar}}
(cli/dispatch-tree tree ["foo" "--bar" "bar" "bar"])))
))
:opts {:bar :bar}
:args ["arg1"]}
(cli/dispatch-tree tree ["foo" "--bar" "bar" "bar" "arg1"])))))

(deftest no-keyword-opts-test (is (= {:query [:a :b :c]}
(cli/parse-opts
Expand Down

0 comments on commit 2dad2e9

Please sign in to comment.