Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/clojure/clojure/tools/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
[specs]
(reduce (fn [m s]
(if (contains? s :default)
(assoc m (:name s) (:default s))
((:assoc-fn s) m (:name s) (:default s))
m))
{} specs))

Expand All @@ -72,12 +72,12 @@
(throw (Exception. (str "'" opt "' is not a valid argument")))

(and (opt? opt) (spec :flag))
(recur (assoc options (spec :name) (flag-for opt))
(recur ((spec :assoc-fn) options (spec :name) (flag-for opt))
extra-args
(rest args))

(opt? opt)
(recur (assoc options (spec :name) ((spec :parse-fn) (second args)))
(recur ((spec :assoc-fn) options (spec :name) ((spec :parse-fn) (second args)))
extra-args
(drop 2 args))

Expand Down Expand Up @@ -105,6 +105,7 @@
:aliases (set aliases)
:name (keyword (last aliases))
:parse-fn identity
:assoc-fn assoc
:flag flag}
(when flag {:default false})
options)))
Expand Down
11 changes: 11 additions & 0 deletions src/test/clojure/clojure/tools/cli_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
(first (cli ["-s" "localhost"]
["-s" "--server"]))))))

(testing "merging args"
(deftest should-merge-identical-arguments
(let [assoc-fn (fn [previous key val]
(assoc previous key
(if-let [oldval (get previous key)]
(merge oldval val)
(hash-set val))))
[options args _] (cli ["-p" "1" "--port" "2"]
["-p" "--port" "description" :assoc-fn assoc-fn])]
(is (= {:port #{"1" "2"}} options)))))

(testing "extra arguments"
(deftest should-provide-access-to-trailing-args
(let [[options args _] (cli ["--foo" "bar" "a" "b" "c"]
Expand Down