Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.62 (2024-12-22)

- Fix [#109](https://github.com/babashka/cli/issues/109): allow options to start with a number

## v0.8.61 (2024-11-15)

- Fix [#102](https://github.com/babashka/cli/issues/102): `format-table` correctly pads cells containing ANSI escape codes
Expand Down
24 changes: 15 additions & 9 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,23 @@
{:args new-args
:args->opts args->opts})))

(defn- parse-key [arg mode current-opt coerce-opt added]
(defn- parse-key [arg mode current-opt coerce-opt added known-keys alias-keys]
(let [fst-char (first-char arg)
snd-char (second-char arg)
hyphen-opt? (and (not= :keywords mode)
(= fst-char \-)
(not (number-char? snd-char)))
(= \- fst-char)
(let [k (keyword (subs arg 1))]
(or
(contains? known-keys k)
(contains? alias-keys k)
(not (number-char? snd-char)))))
mode (or mode (when hyphen-opt? :hyphens))
fst-colon? (= \: fst-char)
kwd-opt? (and (not= :hyphens mode)
fst-colon?
(or (= :boolean coerce-opt)
(or (not current-opt)
(= added current-opt))))
(not current-opt)
(= added current-opt)))
mode (or mode
(when kwd-opt?
:keywords))
Expand Down Expand Up @@ -307,8 +311,10 @@
no-keyword-opts (:no-keyword-opts opts)
restrict (or (:restrict opts)
(:closed opts))
known-keys (set (concat (keys (if (map? spec)
spec (into {} spec)))
spec-map (if (map? spec)
spec (into {} spec))
alias-keys (set (concat (keys aliases) (map :alias (vals spec-map))))
known-keys (set (concat (keys spec-map)
(vals aliases)
(keys coerce-opts)))
restrict (if (true? restrict)
Expand Down Expand Up @@ -362,7 +368,7 @@
{:keys [hyphen-opt
composite-opt
kwd-opt
mode fst-colon]} (parse-key arg mode current-opt coerce-opt added)]
mode fst-colon]} (parse-key arg mode current-opt coerce-opt added known-keys alias-keys)]
(if (or hyphen-opt
kwd-opt)
(let [long-opt? (str/starts-with? arg "--")
Expand All @@ -387,7 +393,7 @@
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 known-keys alias-keys)
negative? (when-not (contains? known-keys k)
(str/starts-with? (str k) ":no-"))]
(if (or (:hyphen-opt m)
Expand Down
5 changes: 4 additions & 1 deletion test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@
(is (= -10 (cli/auto-coerce "-10")))
(is (submap? {:foo -10} (cli/parse-opts ["--foo" "-10"])))
(is (submap? {:foo -10} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :number}})))
(is (submap? {:foo "-10"} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :string}}))))
(is (submap? {:foo "-10"} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :string}})))
(is (submap? {:6 true} (cli/parse-opts ["-6"] {:spec {:6 {}}})))
(is (submap? {:6 true} (cli/parse-opts ["-6"] {:coerce {:6 :boolean}})))
(is (submap? {:ipv6 true} (cli/parse-opts ["-6"] {:aliases {:6 :ipv6}}))))

(deftest format-opts-test
(testing "default width with default and default-desc"
Expand Down
Loading