Skip to content

Commit

Permalink
yolo
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jan 2, 2024
1 parent c537ac9 commit fec4e31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,15 @@
:opts {}})
[arg & rest] args
all-opts (-> (merge all-opts opts)
(assoc-in (cons ::opts-by-cmd cmds) opts))]
(update ::opts-by-cmds (fnil conj []) {:cmds cmds
:opts opts}))]
(if-let [subcmd-info (get (:cmd cmd-info) arg)]
(recur (conj cmds arg) all-opts rest subcmd-info)
(if (:fn cmd-info)
{:cmd-info cmd-info
:dispatch cmds
:opts (dissoc all-opts ::opts-by-cmd)
:opts-tree (::opts-by-cmd all-opts)
:opts (dissoc all-opts ::opts-by-cmds)
:opts-by-cmds (::opts-by-cmds all-opts)
:args args}
(if arg
{:error :no-match
Expand Down
23 changes: 4 additions & 19 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,6 @@
:spec {:quux {:coerce :keyword}}
:fn identity}])))))

;; TODO, test
;; TODO: add [] test
#_(dispatch [{:cmds ["foo" "bar"]
:spec {:foo {:coerce :keyword}}
:fn identity}] ["foo" "bar" "--foo" "dude"])

#_(comment
(dispatch [{:cmds ["foo"] :fn identity}
{:cmds [] :fn identity}]
[])

(dispatch [{:cmds ["foo"] :fn identity}] ["foo"])
(dispatch [{:cmds ["foo" "bar"]
:spec {:foo {:coerce :keyword}}
:fn identity}] ["foo" "bar" "--foo" "dude"])
(dispatch [{:cmds ["foo" "bar" "baz"] :fn identity}] ["foo" "bar" "baz"])
)

(deftest dispatch-tree-test
(d/deflet
(def table [{:cmds ["foo" "bar"]
Expand Down Expand Up @@ -369,7 +351,10 @@
(is (submap?
{:dispatch ["foo" "bar"],
:opts {:foo :dude3},
:opts-tree {:foo :dude1, "foo" {:foo :dude2}},
:opts-by-cmds
[{:cmds [], :opts {:foo :dude1}}
{:cmds ["foo"], :opts {:foo :dude2}}
{:cmds ["foo" "bar"], :opts {:foo :dude3}}],
:args ["bar" "arg1"]}
(cli/dispatch
table
Expand Down

0 comments on commit fec4e31

Please sign in to comment.