|
88 | 88 | (let [mw (if (or (nil? mw) (sequential? mw)) mw [mw])] |
89 | 89 | (update opts ::middleware (fnil into []) mw))) |
90 | 90 |
|
| 91 | +(defn call-handler [handler flags & args] |
| 92 | + (binding [*opts* flags] |
| 93 | + (apply handler flags args))) |
| 94 | + |
91 | 95 | (defn assoc-flag |
92 | 96 | ([flags flagspec] |
93 | 97 | (add-middleware |
94 | 98 | (if-let [handler (:handler flagspec)] |
95 | | - (binding [*opts* flags] (handler flags)) |
| 99 | + (call-handler handler flags) |
96 | 100 | (assoc flags (:key flagspec) (:value flagspec))) |
97 | 101 | flagspec)) |
98 | 102 | ([flags flagspec & args] |
99 | 103 | (add-middleware |
100 | 104 | (if-let [handler (:handler flagspec)] |
101 | | - (binding [*opts* flags] (apply handler flags args)) |
| 105 | + (apply call-handler handler flags args) |
102 | 106 | (assoc flags (:key flagspec) |
103 | 107 | (if (= 1 (count args)) |
104 | 108 | (first args) |
|
108 | 112 | (defn update-flag [flags flagspec f & args] |
109 | 113 | (add-middleware |
110 | 114 | (if-let [handler (:handler flagspec)] |
111 | | - (binding [*opts* flags] (apply handler flags args)) |
| 115 | + (apply call-handler handler flags args) |
112 | 116 | (apply update flags (:key flagspec) f args)) |
113 | 117 | flagspec)) |
114 | 118 |
|
|
243 | 247 | init |
244 | 248 | (map second flagpairs))) |
245 | 249 |
|
246 | | -(defn add-extra-flags |
| 250 | +(defn add-processed-flags |
247 | 251 | "We process flag information for easier use, this results in |
248 | 252 | `:flagpairs` (ordered sequence of pairs, mainly used in printing help |
249 | 253 | information), and `:flagmap` (for easy lookup), added to the `cmdspec`. As we |
|
268 | 272 | (loop [cmdspec cmdspec |
269 | 273 | [arg & cli-args] cli-args |
270 | 274 | args [] |
| 275 | + seen-prefixes #{} |
271 | 276 | flags init] |
272 | 277 | ;; 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)) |
274 | 280 | flags (add-defaults flags (prepare-flagpairs extra-flags)) |
275 | | - cmdspec (add-extra-flags cmdspec extra-flags)] |
| 281 | + cmdspec (add-processed-flags cmdspec extra-flags)] |
276 | 282 | (cond |
277 | 283 | (nil? arg) |
278 | 284 | [cmdspec args flags] |
|
283 | 289 | (and (= \- (first arg)) |
284 | 290 | (not= 1 (count arg))) ; single dash is considered a positional argument |
285 | 291 | (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)) |
287 | 293 |
|
288 | 294 | :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))))) |
290 | 296 |
|
291 | 297 | (defn dispatch |
292 | 298 | "Main entry point for com.lambdaisland/cli. |
|
0 commit comments