From 0d7e3c2c65ea8a047fb89ced3ba224e5e8fa82d4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 2 Jan 2024 16:56:03 +0100 Subject: [PATCH] dude --- src/babashka/cli.cljc | 16 ++++++++++++---- test/babashka/cli_test.cljc | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index b25ed21..cc158d9 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -566,9 +566,17 @@ (defn- table->tree [table] (reduce (fn [tree {:as cfg :keys [cmds]}] - (assoc-in tree (interleave (repeat :cmd) cmds) (dissoc cfg :cmds))) + (let [ks (interleave (repeat :cmd) cmds)] + (if (seq ks) + (assoc-in tree ks (dissoc cfg :cmds)) + ;; catch-all + (merge tree (dissoc cfg :cmds))))) {} table)) +(comment + (table->tree [{:cmds [] :fn identity}]) + ) + (defn- deep-merge [a b] (reduce (fn [acc k] (update acc k (fn [v] (if (map? v) @@ -588,7 +596,7 @@ ([tree args] (dispatch-tree' tree args nil)) ([tree args opts] - (def t tree) + #_(def t tree) (loop [cmds [] all-opts {} args args cmd-info tree] (let [;; cmd-info (:cmd cmd-info) kwm cmd-info #_(select-keys cmd-info (filter keyword? (keys cmd-info))) @@ -614,9 +622,9 @@ (if arg {:error :no-match :wrong-input arg - :available-commands (filter string? (keys cmd-info))} + :available-commands (keys (:cmd cmd-info))} {:error :input-exhausted - :available-commands (filter string? (keys cmd-info))}))))))) + :available-commands (keys (:cmd cmd-info))}))))))) (comment (dispatch [{:cmds ["foo"] :fn identity} diff --git a/test/babashka/cli_test.cljc b/test/babashka/cli_test.cljc index 4ccb14b..f76dd61 100644 --- a/test/babashka/cli_test.cljc +++ b/test/babashka/cli_test.cljc @@ -304,6 +304,7 @@ :fn identity}]))))) ;; TODO, test +;; TODO: add [] test #_(dispatch [{:cmds ["foo" "bar"] :spec {:foo {:coerce :keyword}} :fn identity}] ["foo" "bar" "--foo" "dude"])