Skip to content
Browse files

CLJS: Port cli/summarize

We extract and port the fn format-lines, which uses pprint/cl-format in
Clojure and goog.string.format in CLJS.

This removes the only non-CLJS dependency from the CLJS version.
  • Loading branch information...
1 parent 8e33c0a commit 0b8326dfffc087d29a9927e902850491fd9042c3 @guns guns committed Dec 9, 2013
View
33 src/main/clojure/cljs/tools/cli.cljs
@@ -1,6 +1,8 @@
(ns clojure.tools.cli
{:author "Sung Pae"}
- (:require [clojure.string :as s]))
+ (:require [clojure.string :as s]
+ goog.string.format
+ [goog.string :as gs]))
(defn- tokenize-args
"Reduce arguments sequence into [opt-type opt ?optarg?] vectors and a vector
@@ -188,3 +190,32 @@
[m (conj errors error)]))
[m (conj errors (str "Unknown option: " (pr-str opt)))]))
[(default-option-map specs) []] tokens))
+
+(defn- make-summary-parts [all-boolean? specs]
+ (let [{:keys [short-opt long-opt required default default-desc desc]} specs
+ opt (cond (and short-opt long-opt) (str short-opt ", " long-opt)
+ long-opt (str " " long-opt)
+ short-opt short-opt)
+ [opt dd] (if required
+ [(str opt \space required)
+ (or default-desc (if default (str default) ""))]
+ [opt ""])]
+ (if all-boolean?
+ [opt (or desc "")]
+ [opt dd (or desc "")])))
+
+(defn- format-lines [lens parts]
+ (let [fmt (case (count lens)
+ 2 " %%-%ds %%-%ds"
+ 3 " %%-%ds %%-%ds %%-%ds")
+ fmt (apply gs/format fmt lens)]
+ (map #(s/trimr (apply gs/format fmt %)) parts)))
+
+(defn summarize
+ "Reduce options specs into a options summary for printing at a terminal."
+ [specs]
+ (let [all-boolean? (every? (comp not :required) specs)
+ parts (map (partial make-summary-parts all-boolean?) specs)
+ lens (apply map (fn [& cols] (apply max (map count cols))) parts)
+ lines (format-lines lens parts)]
+ (s/join \newline lines)))
View
12 src/main/clojure/clojure/tools/cli.clj
@@ -358,17 +358,19 @@
[opt (or desc "")]
[opt dd (or desc "")])))
+(defn- format-lines [lens parts]
+ (let [cl-fmt (case (count lens)
+ 2 "~{ ~vA ~vA~}"
+ 3 "~{ ~vA ~vA ~vA~}")]
+ (map #(s/trimr (cl-format nil cl-fmt (interleave lens %))) parts)))
+
(defn summarize
"Reduce options specs into a options summary for printing at a terminal."
[specs]
(let [all-boolean? (every? (comp not :required) specs)
parts (map (partial make-summary-parts all-boolean?) specs)
- cl-fmt (if all-boolean?
- "~{ ~vA ~vA~}"
- "~{ ~vA ~vA ~vA~}")
lens (apply map (fn [& cols] (apply max (map count cols))) parts)
- lines (map #(s/trimr (cl-format nil cl-fmt (interleave lens %)))
- parts)]
+ lines (format-lines lens parts)]
(s/join \newline lines)))
(defn- required-arguments [specs]
View
48 src/test/clojure/clojure/tools/cli_test.clj
@@ -126,30 +126,30 @@
[:long-opt "--verbose"]])
[{:alpha true :verbose 3} []])))))
-; (deftest test-summarize
-; (testing "summarizes options"
-; (is (= (summarize (compile-option-specs
-; [["-s" "--server HOST" "Upstream server"
-; :default :some-object-whose-string-representation-is-awful
-; :default-desc "example.com"]
-; ["-p" "--port=PORT" "Upstream port number"
-; :default 80]
-; ["-o" nil "Output file"
-; :id :output
-; :required "PATH"]
-; ["-v" nil "Verbosity level; may be specified more than once"
-; :id :verbose
-; :default 0]
-; [nil "--help"]]))
-; (join \newline
-; [" -s, --server HOST example.com Upstream server"
-; " -p, --port PORT 80 Upstream port number"
-; " -o PATH Output file"
-; " -v Verbosity level; may be specified more than once"
-; " --help"]))))
-; (testing "does not print :default column when all options are boolean"
-; (is (= (summarize (compile-option-specs [["-m" "--minimal" "A minimal option summary"]]))
-; " -m, --minimal A minimal option summary"))))
+(deftest test-summarize
+ (testing "summarizes options"
+ (is (= (summarize (compile-option-specs
+ [["-s" "--server HOST" "Upstream server"
+ :default :some-object-whose-string-representation-is-awful
+ :default-desc "example.com"]
+ ["-p" "--port=PORT" "Upstream port number"
+ :default 80]
+ ["-o" nil "Output file"
+ :id :output
+ :required "PATH"]
+ ["-v" nil "Verbosity level; may be specified more than once"
+ :id :verbose
+ :default 0]
+ [nil "--help"]]))
+ (join \newline
+ [" -s, --server HOST example.com Upstream server"
+ " -p, --port PORT 80 Upstream port number"
+ " -o PATH Output file"
+ " -v Verbosity level; may be specified more than once"
+ " --help"]))))
+ (testing "does not print :default column when all options are boolean"
+ (is (= (summarize (compile-option-specs [["-m" "--minimal" "A minimal option summary"]]))
+ " -m, --minimal A minimal option summary"))))
; (deftest test-parse-opts
; (testing "parses options to :options"

0 comments on commit 0b8326d

Please sign in to comment.
Something went wrong with that request. Please try again.