Skip to content

Commit

Permalink
Add provenance tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Dec 23, 2024
1 parent 694e529 commit a1f884e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Added

## Fixed

## Changed
- Add provenance tracking, `:lambdaisland.cli/sources` is a map from options key
to human readable description of where that key came from, e.g. `--foo command
line flag`, or `positional command line argument idx=0`. See
[lambdaisland/config](https://github.com/lambdaisland/config) for a use case.

# 0.18.74 (2024-08-05 / 14b74ba)

Expand Down
39 changes: 22 additions & 17 deletions src/lambdaisland/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,23 @@
(cmd
(if handler
(apply call-handler handler opts args)
(assoc opts
(:key flagspec)
(cond
(= 0 (count args))
(if (contains? flagspec :value)
(:value flagspec)
((fnil inc 0) (get opts (:key flagspec))))
(= 1 (count args))
(if (:coll? flagspec)
((fnil conj []) (get opts (:key flagspec)) (first args))
(first args))
:else
(if (:coll? flagspec)
((fnil into []) (get opts (:key flagspec)) args)
(vec args)))))))))))))
(-> opts
(assoc
(:key flagspec)
(cond
(= 0 (count args))
(if (contains? flagspec :value)
(:value flagspec)
((fnil inc 0) (get opts (:key flagspec))))
(= 1 (count args))
(if (:coll? flagspec)
((fnil conj []) (get opts (:key flagspec)) (first args))
(first args))
:else
(if (:coll? flagspec)
((fnil into []) (get opts (:key flagspec)) args)
(vec args))))
(assoc-in [::sources (:key flagspec)] (str (:flag flagspec) " command line flag"))))))))))))

(defn default-parse [s]
(cond
Expand Down Expand Up @@ -294,7 +296,9 @@
(h opts (if (and (string? d) (:parse flagspec))
((:parse flagspec default-parse) d)
d)))
(assoc opts (:key flagspec) d))
(-> opts
(assoc (:key flagspec) d)
(assoc-in [::sources (:key flagspec)] (str (:flagstr flagspec) " (default value)"))))
opts))
init
(map second flagpairs)))
Expand Down Expand Up @@ -436,7 +440,8 @@
(merge (when-let [i (:init cmdspec)]
(if (or (fn? i) (var? i)) (i) i)))
(merge (zipmap argnames pos-args))
))
(update ::sources merge (zipmap argnames (map (fn [idx] (str "positional command line argument idx=" idx))
(range (count pos-args)))))))

(or (nil? command-match)
(:help opts)
Expand Down
9 changes: 6 additions & 3 deletions test/lambdaisland/cli_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
(are [input args expected]
(is (= expected (cli/dispatch* input args)))
(cmdspec-1 false) [] {:lambdaisland.cli/argv []}
(cmdspec-1 true) ["-x"] {:lambdaisland.cli/argv [] :x 1}
(cmdspec-n false) ["run"] {:lambdaisland.cli/argv [] :lambdaisland.cli/command ["run"]}
(cmdspec-n true) ["run" "-x"] {:lambdaisland.cli/argv [] :lambdaisland.cli/command ["run"] :x 1}))
(cmdspec-1 true) ["-x"] {:lambdaisland.cli/argv [] :x 1
:lambdaisland.cli/sources {:x "-x command line flag"}}
(cmdspec-n false) ["run"] {:lambdaisland.cli/argv [] :lambdaisland.cli/command ["run"]
:lambdaisland.cli/sources {}}
(cmdspec-n true) ["run" "-x"] {:lambdaisland.cli/argv [] :lambdaisland.cli/command ["run"] :x 1
:lambdaisland.cli/sources {:x "-x command line flag"}}))

(testing "help exit"
(are [input args expected]
Expand Down

0 comments on commit a1f884e

Please sign in to comment.