From c970a4df04248b88ead121e2a8201fe9fa75b494 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 4 Jan 2024 21:28:50 +0100 Subject: [PATCH] v0.8.55 --- CHANGELOG.md | 4 ++++ src/babashka/cli.cljc | 24 ++++++++++++------------ test/babashka/cli_test.cljc | 4 +++- version.edn | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f94479f..22deb2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ For breaking changes, check [here](#breaking-changes). [Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs! +## v0.8.55 (2024-01-04) + +- Fix `--no-option` (`--no` prefix) in combination with subcommands + ## v0.8.54 (2024-01-04) - Prioritize `:exec-args` over spec `:default`s diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index 3d4c2ac..54b9283 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -337,9 +337,7 @@ [opts last-opt added] (if (and (::dispatch-tree opts) (seq cmds)) - (do - ;; (prn :result-to-dispatch cmds args :> (into (vec cmds) args)) - [(vary-meta {} assoc-in [:org.babashka/cli :args] (into (vec cmds) args)) nil nil]) + [(vary-meta {} assoc-in [:org.babashka/cli :args] (into (vec cmds) args)) nil nil] (loop [acc {} current-opt nil added nil @@ -385,9 +383,12 @@ k nil mode (cons arg-val (rest args)) a->o) (let [next-args (next args) next-arg (first next-args) - m (parse-key next-arg mode current-opt coerce-opt added)] + m (parse-key next-arg mode current-opt coerce-opt added) + negative? (when-not (contains? known-keys k) + (str/starts-with? (str k) ":no-"))] (if (or (:hyphen-opt m) - (empty? next-args)) + (empty? next-args) + negative?) ;; implicit true (if composite-opt (let [chars (name k) @@ -398,9 +399,7 @@ (recur acc nil nil mode next-args a->o)) - (let [negative? (when-not (contains? known-keys k) - (str/starts-with? (str k) ":no-")) - k (if negative? + (let [k (if negative? (keyword (str/replace (str k) ":no-" "")) k) next-args (cons (not negative?) #_"true" next-args)] @@ -621,7 +620,6 @@ ::dispatch-tree-ignored-args (set (keys (:cmd cmd-info))))) {:args args :opts {}}) - ;; _ (prn :dispatch-args-post args) [arg & rest] args all-opts (-> (merge all-opts opts) (update ::opts-by-cmds (fnil conj []) {:cmds cmds @@ -639,9 +637,11 @@ (if arg {:error :no-match :wrong-input arg - :available-commands (keys (:cmd cmd-info))} + :available-commands (keys (:cmd cmd-info)) + :opts (dissoc all-opts ::opts-by-cmds)} {:error :input-exhausted - :available-commands (keys (:cmd cmd-info))}))))))) + :available-commands (keys (:cmd cmd-info)) + :opts (dissoc all-opts ::opts-by-cmds)}))))))) (defn- dispatch-tree ([tree args] @@ -660,7 +660,7 @@ error-fn*))] (case error (:no-match :input-exhausted) - (error-fn {:cause error}) + (error-fn {:cause error :opts (:opts res)}) nil ((:fn cmd-info) (dissoc res :cmd-info)))))) (defn dispatch diff --git a/test/babashka/cli_test.cljc b/test/babashka/cli_test.cljc index 54957f8..411fbd6 100644 --- a/test/babashka/cli_test.cljc +++ b/test/babashka/cli_test.cljc @@ -98,7 +98,9 @@ (cli/parse-opts ["--foo" "-abc"])))) (testing "--no- prefix" (is (= {:option false, :no-this-exists true} - (cli/parse-opts ["--no-option" "--no-this-exists"] {:coerce {:no-this-exists :bool}}))))) + (cli/parse-opts ["--no-option" "--no-this-exists"] {:coerce {:no-this-exists :bool}}))) + (is (= {:args ["dude"], :opts {:option false}} + (cli/parse-args ["--no-option" "dude"] {:coerce {:option :bool}}))))) (deftest parse-opts-keywords-test (is (= {:version "2021a4", :no-git-tag-version true, :deps-file "foo.edn"} diff --git a/version.edn b/version.edn index f78208f..ab047c7 100644 --- a/version.edn +++ b/version.edn @@ -1 +1 @@ -{:major 0, :minor 8, :release 54} +{:major 0, :minor 8, :release 55}