Skip to content

Commit 3b98295

Browse files
committed
Dont let CLI flag defaults override initial context values
1 parent 5eefc7a commit 3b98295

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
## Added
44

5+
- Add source tracking (config provenance) for keys in the initial context
6+
57
## Fixed
68

7-
## Changed
9+
- Only apply flag defaults if they are not yet present in the initial context
810

911
# 0.22.87 (2025-02-09 / afd4eb0)
1012

src/lambdaisland/cli.clj

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,18 @@
296296

297297
(defn add-defaults [init-opts flagpairs]
298298
(reduce (fn [opts flagspec]
299-
(if-let [d (:default flagspec)]
300-
(let [d (if (and (string? d) (:parse flagspec))
301-
((:parse flagspec default-parse) d)
302-
d)]
303-
(if-let [h (:handler flagspec)]
304-
(binding [*opts* opts] (h opts d))
305-
(-> opts
306-
(assoc (:key flagspec) d)
307-
(assoc-in [::sources (:key flagspec)] (str (:flagstr flagspec) " (default value)")))))
308-
opts))
299+
(if (some? (get opts (:key flagspec)))
300+
opts
301+
(if-let [d (:default flagspec)]
302+
(let [d (if (and (string? d) (:parse flagspec))
303+
((:parse flagspec default-parse) d)
304+
d)]
305+
(if-let [h (:handler flagspec)]
306+
(binding [*opts* opts] (h opts d))
307+
(-> opts
308+
(assoc (:key flagspec) d)
309+
(assoc-in [::sources (:key flagspec)] (str (:flagstr flagspec) " (default value)")))))
310+
opts)))
309311
init-opts
310312
(map second flagpairs)))
311313

@@ -397,6 +399,7 @@
397399
(dispatch* (to-cmdspec cmdspec) *command-line-args*))
398400
([{:keys [flags init] :as cmdspec} cli-args]
399401
(let [init (if (or (fn? init) (var? init)) (init) init)
402+
init (assoc init ::sources (into {} (map (fn [k] [k "Initial context"])) (keys init)))
400403
[cmdspec pos-args flags] (split-flags cmdspec cli-args init)
401404
flagpairs (get cmdspec :flagpairs)]
402405
(dispatch* cmdspec pos-args flags)))

0 commit comments

Comments
 (0)