Skip to content

Commit

Permalink
Fix #106: multiple options before subcommand conflict with subcommand (
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Nov 15, 2024
1 parent 2b2404a commit 6ee198e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ For breaking changes, check [here](#breaking-changes).
## Unreleased

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

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

Expand Down
6 changes: 4 additions & 2 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@
(not= arg "true")
(not= arg "false"))
(and (= added current-opt)
(not collect-fn)))]
(or
(not collect-fn)
(contains? (::dispatch-tree-ignored-args opts) (first args)))))]
(if the-end?
(let [{new-args :args
a->o :args->opts}
Expand Down Expand Up @@ -535,7 +537,7 @@
(fn [widths row]
(map max (map str-width row) widths)) (repeat 0) rows)
pad-row (fn [row]
(map (fn [width cell] (pad width cell)) widths row))]
(map pad widths row))]
(map pad-row rows)))

(defn format-table [{:keys [rows indent] :or {indent 2}}]
Expand Down
31 changes: 31 additions & 0 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,34 @@
:spec {:x {:coerce :boolean}}}]
["foo"]
{:restrict true}))))

(deftest issue-106-test
(d/deflet
(def global-spec {:config {:desc "Config edn file to use"
:coerce []}
:verbose {:coerce :boolean}})
(def dns-spec {})
(def dns-get-spec {})
(def table
[{:cmds [] :fn identity :spec global-spec}
{:cmds ["dns"] :fn identity :spec dns-spec}
{:cmds ["dns" "get"] :fn identity :spec dns-get-spec}])
(is (submap?
{:dispatch ["dns"], :opts {:config ["config-dev.edn" "other.edn"]}, :args nil}
(cli/dispatch table ["--config" "config-dev.edn" "--config" "other.edn" "dns"])))
(is (submap?
{:dispatch ["dns" "get"],
:opts {:config ["config-dev.edn" "other.edn"]},
:args nil}
(cli/dispatch table ["--config" "config-dev.edn" "--config" "other.edn" "dns" "get"])))
(is (submap?
{:dispatch ["dns" "get"],
:opts {:config ["config-dev.edn" "other.edn"], :verbose true},
:args nil}
(cli/dispatch table ["--config" "config-dev.edn" "--verbose" "--config" "other.edn" "dns" "get"])))
(is (submap?
{:dispatch ["dns" "get"],
:opts {:config ["config-dev.edn" "other.edn"]},
:args nil}
(cli/dispatch table ["--config" "config-dev.edn" "--config=other.edn" "dns" "get"]))))
)

0 comments on commit 6ee198e

Please sign in to comment.