Skip to content

Commit 4e3e29f

Browse files
committed
Fix #106: multiple options before subcommand conflict with subcommand
1 parent 2b2404a commit 4e3e29f

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ For breaking changes, check [here](#breaking-changes).
77
## Unreleased
88

99
- Fix [#102](https://github.com/babashka/cli/issues/102): `format-table` correctly pads cells containing ANSI escape codes
10+
- Fix [#106](https://github.com/babashka/cli/issues/106): Multiple options before subcommand conflict with subcommand
1011

1112
## v0.8.60 (2024-07-23)
1213

src/babashka/cli.cljc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@
418418
(not= arg "true")
419419
(not= arg "false"))
420420
(and (= added current-opt)
421-
(not collect-fn)))]
421+
(or
422+
(not collect-fn)
423+
(contains? (::dispatch-tree-ignored-args opts) (first args)))))]
422424
(if the-end?
423425
(let [{new-args :args
424426
a->o :args->opts}
@@ -535,7 +537,7 @@
535537
(fn [widths row]
536538
(map max (map str-width row) widths)) (repeat 0) rows)
537539
pad-row (fn [row]
538-
(map (fn [width cell] (pad width cell)) widths row))]
540+
(map pad widths row))]
539541
(map pad-row rows)))
540542

541543
(defn format-table [{:keys [rows indent] :or {indent 2}}]

test/babashka/cli_test.cljc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,34 @@
587587
:spec {:x {:coerce :boolean}}}]
588588
["foo"]
589589
{:restrict true}))))
590+
591+
(deftest issue-106-test
592+
(d/deflet
593+
(def global-spec {:config {:desc "Config edn file to use"
594+
:coerce []}
595+
:verbose {:coerce :boolean}})
596+
(def dns-spec {})
597+
(def dns-get-spec {})
598+
(def table
599+
[{:cmds [] :fn identity :spec global-spec}
600+
{:cmds ["dns"] :fn identity :spec dns-spec}
601+
{:cmds ["dns" "get"] :fn identity :spec dns-get-spec}])
602+
(is (submap?
603+
{:dispatch ["dns"], :opts {:config ["config-dev.edn" "other.edn"]}, :args nil}
604+
(cli/dispatch table ["--config" "config-dev.edn" "--config" "other.edn" "dns"])))
605+
(is (submap?
606+
{:dispatch ["dns" "get"],
607+
:opts {:config ["config-dev.edn" "other.edn"]},
608+
:args nil}
609+
(cli/dispatch table ["--config" "config-dev.edn" "--config" "other.edn" "dns" "get"])))
610+
(is (submap?
611+
{:dispatch ["dns" "get"],
612+
:opts {:config ["config-dev.edn" "other.edn"], :verbose true},
613+
:args nil}
614+
(cli/dispatch table ["--config" "config-dev.edn" "--verbose" "--config" "other.edn" "dns" "get"])))
615+
(is (submap?
616+
{:dispatch ["dns" "get"],
617+
:opts {:config ["config-dev.edn" "other.edn"]},
618+
:args nil}
619+
(cli/dispatch table ["--config" "config-dev.edn" "--config=other.edn" "dns" "get"]))))
620+
)

0 commit comments

Comments
 (0)