Permalink
Browse files

Adding maven bits and renaming clargon to cli

  • Loading branch information...
1 parent 3399597 commit b08f1002bd2be33664a93150cfe41e1439a0b258 @abedra abedra committed Jun 21, 2011
Showing with 76 additions and 63 deletions.
  1. +1 −0 .gitignore
  2. +3 −4 README.md
  3. +24 −0 pom.xml
  4. +3 −8 src/main/clojure/clojure/tools/cli.clj
  5. +45 −51 src/test/clojure/clojure/tools/cli_test.clj
View
@@ -0,0 +1 @@
+target
View
@@ -1,14 +1,13 @@
# tools.cli
-Clargon is a Command Line ARG parser...ON. An attempt at creating a
-OptParser-like thing for clojure, but with the added bonus of nested
-groups of arguments.
+tools.cli is a command line argument parser, with the added bonus of
+nested groups of arguments.
## Usage
Example:
- (clargon args
+ (cli args
(required ["-p" "--port" "Listen on this port"] #(Integer. %))
(optional ["--host" "The hostname" :default "localhost"])
(optional ["--verbose" :default true])
View
@@ -0,0 +1,24 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>tools.cli</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ <name>${artifactId}</name>
+
+ <parent>
+ <groupId>org.clojure</groupId>
+ <artifactId>pom.contrib</artifactId>
+ <version>0.0.21</version>
+ </parent>
+
+ <developers>
+ <developer>
+ <name>Gareth Jones</name>
+ </developer>
+ </developers>
+
+ <scm>
+ <connection>scm:git:git@github.com:clojure/tools.cli.git</connection>
+ <developerConnection>scm:git:git@github.com:clojure/tools.cli.git</developerConnection>
+ <url>git@github.com:clojure/tools.cli.git</url>
+ </scm>
+</project>
@@ -1,12 +1,9 @@
-(ns clojure.tools.cli
- #^{:author "Gareth Jones"
- :doc ""}
+(ns ^{:author "Gareth Jones"}
+ clojure.tools.cli
(:use [clojure.string :only (replace)]
[clojure.pprint :only (pprint cl-format)])
(:refer-clojure :exclude [replace]))
-;; help message stuff
-
(defn build-doc [{:keys [switches docs options]}]
[(apply str (interpose ", " switches))
(or (str (options :default)) "")
@@ -28,8 +25,6 @@
(cl-format true "~{ ~vA ~vA ~vA ~vA ~}" v)
(prn))))
-;; option parsing
-
(defn print-and-fail [msg]
(println msg)
(System/exit 1))
@@ -130,7 +125,7 @@
(str parent name))]
(map #(% full-name args) spec-fns))))
-(defn clargon
+(defn cli
"Takes a list of args from the command line and applies the spec-fns
to generate a map of options.
@@ -1,85 +1,83 @@
(ns clojure.tools.cli-test
- (:use [clojure.tools.cli] :reload)
- (:use [clojure.test]))
+ (:use [clojure.test]
+ [clojure.tools.cli]))
(testing "help"
-
(deftest should-print-help-shortform
(let [help-called (atom nil)]
(binding [help-and-quit (partial reset! help-called)]
- (clargon '("-h") (optional ["--port"]))
+ (cli '("-h") (optional ["--port"]))
(is (not (nil? @help-called))))))
(deftest should-print-help-longform
(let [help-called (atom nil)]
(binding [help-and-quit (partial reset! help-called)]
- (clargon '("--help") (optional ["--port"]))
+ (cli '("--help") (optional ["--port"]))
(is (not (nil? @help-called)))))))
(testing "syntax"
-
(deftest should-handle-simple-strings
(is (= {:host "localhost"}
- (clargon '("--host" "localhost") (optional ["--host"])))))
+ (cli '("--host" "localhost") (optional ["--host"])))))
(testing "booleans"
(deftest should-handle-trues
(is (= {:verbose true}
- (clargon '("--verbose") (optional ["--verbose"])))))
+ (cli '("--verbose") (optional ["--verbose"])))))
(deftest should-handle-falses
(is (= {:verbose false}
- (clargon '("--no-verbose") (optional ["--verbose"]))))))
+ (cli '("--no-verbose") (optional ["--verbose"]))))))
(testing "default values"
(deftest should-default-when-no-value
(is (= {:server "10.0.1.10"}
- (clargon '() (optional ["--server" :default "10.0.1.10"])))))
+ (cli '() (optional ["--server" :default "10.0.1.10"])))))
(deftest should-override-when-supplied
(is (= {:server "127.0.0.1"}
- (clargon '("--server" "127.0.0.1") (optional ["--server" :default "10.0.1.10"]))))))
+ (cli '("--server" "127.0.0.1") (optional ["--server" :default "10.0.1.10"]))))))
(deftest should-apply-parse-fn
(is (= {:names ["john" "jeff" "steve"]}
- (clargon '("--names" "john,jeff,steve")
- (optional ["--names"] #(vec (.split % ",")))))))
+ (cli '("--names" "john,jeff,steve")
+ (optional ["--names"] #(vec (.split % ",")))))))
(testing "aliases"
(deftest should-support-multiple-aliases
(is (= {:server "localhost"}
- (clargon '("-s" "localhost")
- (optional ["-s" "--server"])))))
+ (cli '("-s" "localhost")
+ (optional ["-s" "--server"])))))
(deftest should-use-last-alias-provided-as-name-in-map
(is (= {:sizzle "localhost"}
- (clargon '("-s" "localhost")
- (optional ["-s" "--server" "--sizzle"]))))))
+ (cli '("-s" "localhost")
+ (optional ["-s" "--server" "--sizzle"]))))))
(testing "required"
(deftest should-succeed-when-provided
- (clargon '("--server" "localhost")
- (required ["--server"])))
+ (cli '("--server" "localhost")
+ (required ["--server"])))
(deftest should-fail-when-not-provided
(try
(binding [print-and-fail (fn [x] (throw (Exception. "success!")))]
- (is (thrown-with-msg? Exception #"success!"
- (clargon '() (required ["--server"]))))))))
+ (is (thrown-with-msg? Exception #"success!"
+ (cli '() (required ["--server"]))))))))
(testing "grouped parameters"
(deftest should-support-groups
(is (= {:server {:name "localhost"
:port 9090}}
- (clargon '("--server--name" "localhost" "--server--port" "9090")
- (group "--server"
- (optional ["--name"])
- (optional ["--port"] #(Integer. %)))))))
+ (cli '("--server--name" "localhost" "--server--port" "9090")
+ (group "--server"
+ (optional ["--name"])
+ (optional ["--port"] #(Integer. %)))))))
(deftest should-support-nested-groups
(is (= {:servers {:client {:host {:name "localhost" :port 1234}}}}
- (clargon '("--servers--client--host--name" "localhost")
- (group "--servers"
- (group "--client"
- (group "--host"
- (optional ["--name"])
- (optional ["--port" :default 1234]))))))))))
+ (cli '("--servers--client--host--name" "localhost")
+ (group "--servers"
+ (group "--client"
+ (group "--host"
+ (optional ["--name"])
+ (optional ["--port" :default 1234]))))))))))
(deftest all-together-now
(is (= {:port 8080
@@ -90,23 +88,19 @@
:port 9090
:paths {:inbound "/dev/null"
:outbound "/tmp/outbound"}}}
- (clargon '("-p" "8080"
- "--no-verbose"
- "--log-directory" "/tmp"
- "--server--name" "localhost"
- "--server--port" "9090"
- "--server--paths--inbound" "/dev/null")
- (required ["-p" "--port"] #(Integer. %))
- (optional ["--host" :default "localhost"])
- (optional ["--verbose" :default true])
- (optional ["--log-directory" :default "/some/path"])
- (group "--server"
- (optional ["-n" "--name"])
- (optional ["-p" "--port"] #(Integer. %))
- (group "--paths"
- (optional ["--inbound" :default "/tmp/inbound"])
- (optional ["--outbound" :default "/tmp/outbound"])))))))
-
-
-
-
+ (cli '("-p" "8080"
+ "--no-verbose"
+ "--log-directory" "/tmp"
+ "--server--name" "localhost"
+ "--server--port" "9090"
+ "--server--paths--inbound" "/dev/null")
+ (required ["-p" "--port"] #(Integer. %))
+ (optional ["--host" :default "localhost"])
+ (optional ["--verbose" :default true])
+ (optional ["--log-directory" :default "/some/path"])
+ (group "--server"
+ (optional ["-n" "--name"])
+ (optional ["-p" "--port"] #(Integer. %))
+ (group "--paths"
+ (optional ["--inbound" :default "/tmp/inbound"])
+ (optional ["--outbound" :default "/tmp/outbound"])))))))

0 comments on commit b08f100

Please sign in to comment.