Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

97 lines (86 sloc) 3.731 kB
(:use [clojure.test]
(testing "syntax"
(deftest should-handle-simple-strings
(is (= {:host "localhost"}
(first (cli ["--host" "localhost"]
(testing "booleans"
(deftest should-handle-trues
(is (= {:verbose true}
(first (cli ["--verbose"]
(deftest should-handle-falses
(is (= {:verbose false}
(first (cli ["--no-verbose"]
(testing "explicit syntax"
(is (= {:verbose true}
(first (cli ["--verbose"]
["--verbose" :flag true]))))
(is (= {:verbose false}
(first (cli ["--no-verbose"]
["--verbose" :flag true]))))))
(testing "default values"
(deftest should-default-when-no-value
(is (= {:server ""}
(first (cli []
["--server" :default ""])))))
(deftest should-override-when-supplied
(is (= {:server ""}
(first (cli ["--server" ""]
["--server" :default ""])))))
(deftest should-omit-key-when-no-default
(is (= false
(contains? (cli ["--server" ""]
["--server" :default ""]
(deftest should-apply-parse-fn
(is (= {:names ["john" "jeff" "steve"]}
(first (cli ["--names" "john,jeff,steve"]
["--names" :parse-fn #(vec (.split % ","))])))))
(testing "aliases"
(deftest should-support-multiple-aliases
(is (= {:server "localhost"}
(first (cli ["-s" "localhost"]
["-s" "--server"])))))
(deftest should-use-last-alias-provided-as-name-in-map
(is (= {:server "localhost"}
(first (cli ["-s" "localhost"]
["-s" "--server"]))))))
(testing "extra arguments"
(deftest should-provide-access-to-trailing-args
(let [[options args _] (cli ["--foo" "bar" "a" "b" "c"]
["-f" "--foo"])]
(is (= {:foo "bar"} options))
(is (= ["a" "b" "c"] args))))
(deftest should-work-with-trailing-boolean-args
(let [[options args _] (cli ["--no-verbose" "some-file"]
(is (= {:verbose false}))
(is (= ["some-file"] args))))
(deftest should-accept-double-hyphen-as-end-of-args
(let [[options args _] (cli ["--foo" "bar" "--verbose" "--" "file" "-x" "other"]
(is (= {:foo "bar" :verbose true} options))
(is (= ["file" "-x" "other"] args))))))
(deftest all-together-now
(let [[options args _] (cli ["-p" "8080"
"--log-directory" "/tmp"
"--server" "localhost"
["-p" "--port" :parse-fn #(Integer. %)]
["--host" :default "localhost"]
["--[no-]verbose" :default true]
["--log-directory" :default "/some/path"]
(is (= {:port 8080
:host "localhost"
:verbose false
:log-directory "/tmp"
:server "localhost"} options))
(is (= ["filename"] args))))
Jump to Line
Something went wrong with that request. Please try again.