From 4e3e29f75157ff2c81372be437abcaf18c438565 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 15 Nov 2024 20:09:48 +0100 Subject: [PATCH] Fix #106: multiple options before subcommand conflict with subcommand --- CHANGELOG.md | 1 + src/babashka/cli.cljc | 6 ++++-- test/babashka/cli_test.cljc | 31 +++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dc77a9..5ba9e03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index e4ba254..ecd5532 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -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} @@ -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}}] diff --git a/test/babashka/cli_test.cljc b/test/babashka/cli_test.cljc index b286675..e0211dc 100644 --- a/test/babashka/cli_test.cljc +++ b/test/babashka/cli_test.cljc @@ -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"])))) + )