Browse files

Only include defaults column in summary when some will be displayed

Boolean options do not show their default values, and not all options
with required arguments specify a default!
  • Loading branch information...
1 parent de2786e commit e9816bf3361b9a2de5601daf402a3cc435034dd9 @guns guns committed Dec 9, 2013
View
12 src/main/clojure/cljs/tools/cli.cljs
@@ -191,7 +191,7 @@
[m (conj errors (str "Unknown option: " (pr-str opt)))]))
[(default-option-map specs) []] tokens))
-(defn- make-summary-parts [all-boolean? specs]
+(defn- make-summary-parts [show-defaults? 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)
@@ -200,9 +200,9 @@
[(str opt \space required)
(or default-desc (if default (str default) ""))]
[opt ""])]
- (if all-boolean?
- [opt (or desc "")]
- [opt dd (or desc "")])))
+ (if show-defaults?
+ [opt dd (or desc "")]
+ [opt (or desc "")])))
(defn- format-lines [lens parts]
(let [fmt (case (count lens)
@@ -214,8 +214,8 @@
(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)
+ (let [show-defaults? (some #(and (:required %) (:default %)) specs)
+ parts (map (partial make-summary-parts show-defaults?) 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
@@ -345,7 +345,7 @@
[m (conj errors (str "Unknown option: " (pr-str opt)))]))
[(default-option-map specs) []] tokens))
-(defn- make-summary-parts [all-boolean? specs]
+(defn- make-summary-parts [show-defaults? 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)
@@ -354,9 +354,9 @@
[(str opt \space required)
(or default-desc (if default (str default) ""))]
[opt ""])]
- (if all-boolean?
- [opt (or desc "")]
- [opt dd (or desc "")])))
+ (if show-defaults?
+ [opt dd (or desc "")]
+ [opt (or desc "")])))
(defn- format-lines [lens parts]
(let [cl-fmt (case (count lens)
@@ -367,8 +367,8 @@
(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)
+ (let [show-defaults? (some #(and (:required %) (:default %)) specs)
+ parts (map (partial make-summary-parts show-defaults?) specs)
lens (apply map (fn [& cols] (apply max (map count cols))) parts)
lines (format-lines lens parts)]
(s/join \newline lines)))
View
9 src/test/clojure/clojure/tools/cli_test.clj
@@ -154,9 +154,12 @@
" -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"))))
+ (testing "does not print :default column when no defaults will be shown"
+ (is (= (summarize (compile-option-specs [["-b" "--boolean" "A boolean option with a hidden default"
+ :default true]
+ ["-o" "--option ARG" "An option without a default"]]))
+ (join \newline [" -b, --boolean A boolean option with a hidden default"
+ " -o, --option ARG An option without a default"])))))
(deftest test-parse-opts
(testing "parses options to :options"

0 comments on commit e9816bf

Please sign in to comment.