From 7c5b207acf0e7e0619903617d64c5f953c321a93 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 22 Feb 2024 13:48:45 +0100 Subject: [PATCH] Fix #82: prefer alias over composite option (#83) --- CHANGELOG.md | 4 ++++ src/babashka/cli.cljc | 5 +++-- test/babashka/cli_test.cljc | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9fe264..9fcbbe5 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! +## Unreleased + +Fix [#82](https://github.com/babashka/cli/issues/82): prefer alias over composite option + ## v0.8.56 (2024-02-13) - Add `:opts` to `:error-fn` input diff --git a/src/babashka/cli.cljc b/src/babashka/cli.cljc index 2130fe6..1bc69ab 100644 --- a/src/babashka/cli.cljc +++ b/src/babashka/cli.cljc @@ -377,7 +377,8 @@ (str/split kname #"=") [kname]) raw-k (keyword kname) - k (get aliases raw-k raw-k)] + alias (get aliases raw-k) + k (or alias raw-k)] (if arg-val (recur (process-previous acc current-opt added collect-fn) k nil mode (cons arg-val (rest args)) a->o) @@ -390,7 +391,7 @@ (empty? next-args) negative?) ;; implicit true - (if composite-opt + (if (and (not alias) composite-opt) (let [chars (name k) args (mapcat (fn [char] [(str "-" char) true]) diff --git a/test/babashka/cli_test.cljc b/test/babashka/cli_test.cljc index 67de968..5dde95c 100644 --- a/test/babashka/cli_test.cljc +++ b/test/babashka/cli_test.cljc @@ -540,3 +540,8 @@ :exec-args {:dude [:baz]}}))) (is (= {:foo [:bar]} (cli/parse-opts ["--foo" ":bar"] {:coerce {:foo []} :exec-args {:foo [:baz]}})))) + +(deftest issue-82-alias-preference + (is (= {:opts {:verbose2 true}} + (cli/parse-args ["-vv"] {:spec {:verbose1 {:alias :v} + :verbose2 {:alias :vv}}}))))