Permalink
Browse files

add combiner-fn (for doing things like accepting multiple instances o…

…f the same switch
  • Loading branch information...
1 parent ec7bdb3 commit b268dbaedd749088b3e0ae7cee3c8535ea79d9a3 @nathanmarz committed May 9, 2012
Showing with 14 additions and 2 deletions.
  1. +7 −2 src/main/clojure/clojure/tools/cli.clj
  2. +7 −0 src/test/clojure/clojure/tools/cli_test.clj
@@ -51,6 +51,10 @@
[specs]
(into {} (for [s specs] [(s :name) (s :default)])))
+(defn- update-options [options spec val]
+ (let [parsed ((spec :parse-fn) val)]
+ (update-in options [(spec :name)] #((spec :combine-fn) % parsed))))
+
(defn- apply-specs
[specs args]
(loop [options (default-values-for specs)
@@ -73,7 +77,7 @@
(rest args))
(opt? opt)
- (recur (assoc options (spec :name) ((spec :parse-fn) (second args)))
+ (recur (update-options options spec (second args))
extra-args
(drop 2 args))
@@ -102,7 +106,8 @@
:name (keyword (last aliases))
:parse-fn identity
:default (if flag false nil)
- :flag flag}
+ :flag flag
+ :combine-fn (fn [old new] new)}
options)))
(defn cli
@@ -72,6 +72,13 @@
(is (= {:foo "bar" :verbose true} options))
(is (= ["file" "-x" "other"] args))))))
+(deftest combiner-options
+ (let [[options _ _] (cli ["-a" "bar" "-e" "apple" "-a" "foo" "-e" "banana" "-e" "carrot"]
+ ["-a"]
+ ["-e" :combine-fn concat :parse-fn vector])]
+ (is (= {:a "foo" :e ["apple" "banana" "carrot"]} options))
+ ))
+
(deftest all-together-now
(let [[options args _] (cli ["-p" "8080"
"--no-verbose"

0 comments on commit b268dba

Please sign in to comment.