Skip to content

Commit 449155f

Browse files
committed
When having named positional arguments and trailing unnamed positional arguments, assign them correctly
1 parent da2d75c commit 449155f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Unreleased
22

3-
## Added
4-
53
## Fixed
64

7-
## Changed
5+
- When having named positional arguments and trailing unnamed positional arguments, assign them correctly
86

97
# 0.23.93 (2025-02-25 / a192123)
108

src/lambdaisland/cli.clj

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@
142142
(apply call-handler handler opts args)
143143
(-> opts
144144
(assoc
145-
(:key flagspec)
146-
(cond
147-
(= 0 (count args))
148-
(if (contains? flagspec :value)
149-
(:value flagspec)
150-
((fnil inc 0) (get opts (:key flagspec))))
151-
(= 1 (count args))
152-
(if (:coll? flagspec)
153-
((fnil conj []) (get opts (:key flagspec)) (first args))
154-
(first args))
155-
:else
156-
(if (:coll? flagspec)
157-
((fnil into []) (get opts (:key flagspec)) args)
158-
(vec args))))
145+
(:key flagspec)
146+
(cond
147+
(= 0 (count args))
148+
(if (contains? flagspec :value)
149+
(:value flagspec)
150+
((fnil inc 0) (get opts (:key flagspec))))
151+
(= 1 (count args))
152+
(if (:coll? flagspec)
153+
((fnil conj []) (get opts (:key flagspec)) (first args))
154+
(first args))
155+
:else
156+
(if (:coll? flagspec)
157+
((fnil into []) (get opts (:key flagspec)) args)
158+
(vec args))))
159159
(assoc-in [::sources (:key flagspec)] (str (:flag flagspec) " command line flag"))))))))))))
160160

161161
(defn default-parse [s]
@@ -451,6 +451,7 @@
451451
(-> cmdspec
452452
(dissoc :command :commands :middleware)
453453
(merge command-match)
454+
(dissoc :argnames)
454455
(assoc :name (str program-name " " cmd)))
455456
(drop arg-count pos-args)
456457
(-> opts

test/lambdaisland/cli_test.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
:flags ["-x" {:doc "flag x"
1919
:required required}]}]})
2020

21+
(defn cmdspec-pos
22+
"cmdspec with a positional argument."
23+
[]
24+
{:commands ["run <module>"
25+
{:command #'identity}]})
26+
2127
(deftest required-flag
2228
(testing "successful exit"
2329
(are [input args expected]
@@ -47,3 +53,9 @@
4753
(is (thrown-with-msg? Exception expected (cli/dispatch* input args)))
4854
(cmdspec-1 true) [] #"Missing required flags: -x"
4955
(cmdspec-n true) ["run"] #"Missing required flags: -x")))
56+
57+
(deftest positional-argument-with-trailing-arguments
58+
(let [{::cli/keys [argv]
59+
:keys [module]} (cli/dispatch* (cmdspec-pos) ["run" "foo" "bar"])]
60+
(is (= "foo" module))
61+
(is (= ["foo" "bar"] argv))))

0 commit comments

Comments
 (0)