Skip to content

Commit 5a1e316

Browse files
committed
Prevent flag handlers from being called twice
This way defaults don't override values set by handlers
1 parent 64383b5 commit 5a1e316

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/lambdaisland/cli.clj

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,21 @@
8888
(let [mw (if (or (nil? mw) (sequential? mw)) mw [mw])]
8989
(update opts ::middleware (fnil into []) mw)))
9090

91+
(defn call-handler [handler flags & args]
92+
(binding [*opts* flags]
93+
(apply handler flags args)))
94+
9195
(defn assoc-flag
9296
([flags flagspec]
9397
(add-middleware
9498
(if-let [handler (:handler flagspec)]
95-
(binding [*opts* flags] (handler flags))
99+
(call-handler handler flags)
96100
(assoc flags (:key flagspec) (:value flagspec)))
97101
flagspec))
98102
([flags flagspec & args]
99103
(add-middleware
100104
(if-let [handler (:handler flagspec)]
101-
(binding [*opts* flags] (apply handler flags args))
105+
(apply call-handler handler flags args)
102106
(assoc flags (:key flagspec)
103107
(if (= 1 (count args))
104108
(first args)
@@ -108,7 +112,7 @@
108112
(defn update-flag [flags flagspec f & args]
109113
(add-middleware
110114
(if-let [handler (:handler flagspec)]
111-
(binding [*opts* flags] (apply handler flags args))
115+
(apply call-handler handler flags args)
112116
(apply update flags (:key flagspec) f args))
113117
flagspec))
114118

@@ -243,7 +247,7 @@
243247
init
244248
(map second flagpairs)))
245249

246-
(defn add-extra-flags
250+
(defn add-processed-flags
247251
"We process flag information for easier use, this results in
248252
`:flagpairs` (ordered sequence of pairs, mainly used in printing help
249253
information), and `:flagmap` (for easy lookup), added to the `cmdspec`. As we
@@ -268,11 +272,13 @@
268272
(loop [cmdspec cmdspec
269273
[arg & cli-args] cli-args
270274
args []
275+
seen-prefixes #{}
271276
flags init]
272277
;; Handle additional flags by nested commands
273-
(let [extra-flags (cmd->flags cmdspec args)
278+
(let [extra-flags (when-not (seen-prefixes args)
279+
(cmd->flags cmdspec args))
274280
flags (add-defaults flags (prepare-flagpairs extra-flags))
275-
cmdspec (add-extra-flags cmdspec extra-flags)]
281+
cmdspec (add-processed-flags cmdspec extra-flags)]
276282
(cond
277283
(nil? arg)
278284
[cmdspec args flags]
@@ -283,10 +289,10 @@
283289
(and (= \- (first arg))
284290
(not= 1 (count arg))) ; single dash is considered a positional argument
285291
(let [[cli-args args flags] (handle-flag cmdspec arg cli-args args flags)]
286-
(recur (dissoc cmdspec :flags) cli-args args flags))
292+
(recur (dissoc cmdspec :flags) cli-args args (conj seen-prefixes args) flags))
287293

288294
:else
289-
(recur (dissoc cmdspec :flags) cli-args (conj args (str/replace arg #"^\\(.)" (fn [[_ o]] o))) flags)))))
295+
(recur (dissoc cmdspec :flags) cli-args (conj args (str/replace arg #"^\\(.)" (fn [[_ o]] o))) (conj seen-prefixes args) flags)))))
290296

291297
(defn dispatch
292298
"Main entry point for com.lambdaisland/cli.

0 commit comments

Comments
 (0)