Skip to content

Commit cec7b2b

Browse files
authored
Fix #109: allow options to start with a number (#110)
1 parent 811a2b8 commit cec7b2b

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ For breaking changes, check [here](#breaking-changes).
44

55
[Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs!
66

7+
## v0.8.62 (2024-12-22)
8+
9+
- Fix [#109](https://github.com/babashka/cli/issues/109): allow options to start with a number
10+
711
## v0.8.61 (2024-11-15)
812

913
- Fix [#102](https://github.com/babashka/cli/issues/102): `format-table` correctly pads cells containing ANSI escape codes

src/babashka/cli.cljc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,23 @@
233233
{:args new-args
234234
:args->opts args->opts})))
235235

236-
(defn- parse-key [arg mode current-opt coerce-opt added]
236+
(defn- parse-key [arg mode current-opt coerce-opt added known-keys alias-keys]
237237
(let [fst-char (first-char arg)
238238
snd-char (second-char arg)
239239
hyphen-opt? (and (not= :keywords mode)
240-
(= fst-char \-)
241-
(not (number-char? snd-char)))
240+
(= \- fst-char)
241+
(let [k (keyword (subs arg 1))]
242+
(or
243+
(contains? known-keys k)
244+
(contains? alias-keys k)
245+
(not (number-char? snd-char)))))
242246
mode (or mode (when hyphen-opt? :hyphens))
243247
fst-colon? (= \: fst-char)
244248
kwd-opt? (and (not= :hyphens mode)
245249
fst-colon?
246250
(or (= :boolean coerce-opt)
247-
(or (not current-opt)
248-
(= added current-opt))))
251+
(not current-opt)
252+
(= added current-opt)))
249253
mode (or mode
250254
(when kwd-opt?
251255
:keywords))
@@ -307,8 +311,10 @@
307311
no-keyword-opts (:no-keyword-opts opts)
308312
restrict (or (:restrict opts)
309313
(:closed opts))
310-
known-keys (set (concat (keys (if (map? spec)
311-
spec (into {} spec)))
314+
spec-map (if (map? spec)
315+
spec (into {} spec))
316+
alias-keys (set (concat (keys aliases) (map :alias (vals spec-map))))
317+
known-keys (set (concat (keys spec-map)
312318
(vals aliases)
313319
(keys coerce-opts)))
314320
restrict (if (true? restrict)
@@ -362,7 +368,7 @@
362368
{:keys [hyphen-opt
363369
composite-opt
364370
kwd-opt
365-
mode fst-colon]} (parse-key arg mode current-opt coerce-opt added)]
371+
mode fst-colon]} (parse-key arg mode current-opt coerce-opt added known-keys alias-keys)]
366372
(if (or hyphen-opt
367373
kwd-opt)
368374
(let [long-opt? (str/starts-with? arg "--")
@@ -387,7 +393,7 @@
387393
k nil mode (cons arg-val (rest args)) a->o)
388394
(let [next-args (next args)
389395
next-arg (first next-args)
390-
m (parse-key next-arg mode current-opt coerce-opt added)
396+
m (parse-key next-arg mode current-opt coerce-opt added known-keys alias-keys)
391397
negative? (when-not (contains? known-keys k)
392398
(str/starts-with? (str k) ":no-"))]
393399
(if (or (:hyphen-opt m)

test/babashka/cli_test.cljc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@
466466
(is (= -10 (cli/auto-coerce "-10")))
467467
(is (submap? {:foo -10} (cli/parse-opts ["--foo" "-10"])))
468468
(is (submap? {:foo -10} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :number}})))
469-
(is (submap? {:foo "-10"} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :string}}))))
469+
(is (submap? {:foo "-10"} (cli/parse-opts ["--foo" "-10"] {:coerce {:foo :string}})))
470+
(is (submap? {:6 true} (cli/parse-opts ["-6"] {:spec {:6 {}}})))
471+
(is (submap? {:6 true} (cli/parse-opts ["-6"] {:coerce {:6 :boolean}})))
472+
(is (submap? {:ipv6 true} (cli/parse-opts ["-6"] {:aliases {:6 :ipv6}}))))
470473

471474
(deftest format-opts-test
472475
(testing "default width with default and default-desc"

0 commit comments

Comments
 (0)