Skip to content

Commit 23828b0

Browse files
committed
Fix TCLI-97 by allowing validation msg to be a function
1 parent c84e206 commit 23828b0

5 files changed

Lines changed: 12 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
target
22
/.lein-failures
3+
/.lsp
34
/.cpcache
45
/cljs-test-runner-out
56
/.lein-repl-history

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
* Release 1.0.next (on **master**)
4+
* Allow validation message to be a function (of the invalid argument) in addition to being a plain string [TCLI-97](https://clojure.atlassian.net/browse/TCLI-97).
45
* Add `:multi true` to modify behavior of `:update-fn` [TCLI-96](https://clojure.atlassian.net/browse/TCLI-96).
56

67
* Release 1.0.194 2020-02-20

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ be useful within your own `:summary-fn` for generating the default summary.
174174

175175
There is a new option entry `:validate`, which takes a tuple of
176176
`[validation-fn validation-msg]`. The validation-fn receives an option's
177-
argument *after* being parsed by `:parse-fn` if it exists.
177+
argument *after* being parsed by `:parse-fn` if it exists. The validation-msg
178+
can either be a string or a function of one argument that can be called on
179+
the invalid option argument to produce a string:
178180

179181
["-p" "--port PORT" "A port number"
180182
:parse-fn #(Integer/parseInt %)
181-
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
183+
:validate [#(< 0 % 0x10000) #(str % " is not a number between 0 and 65536")]]
182184

183185
If the validation-fn returns a falsey value, the validation-msg is added to the
184186
errors vector.

src/main/clojure/clojure/tools/cli.cljc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
; #(not (.isMulticastAddress %)]
306306
:validate-msg [String] ; [\"Must be an IPv4 host\"
307307
; \"Must not be a multicast address\"]
308+
; can also be a function (of the invalid argument)
308309
:missing String ; \"server must be specified\"
309310
}
310311
@@ -383,7 +384,7 @@
383384

384385
(defn- validation-error [opt optarg msg]
385386
(str "Failed to validate " (pr-join opt optarg)
386-
(if msg (str ": " msg) "")))
387+
(if msg (str ": " (if (string? msg) msg (msg optarg))) "")))
387388

388389
(defn- validate [value spec opt optarg]
389390
(let [{:keys [validate-fn validate-msg]} spec]
@@ -707,7 +708,8 @@
707708
708709
:validate-msg A vector of error messages corresponding to :validate-fn
709710
that will be added to the :errors vector on validation
710-
failure.
711+
failure. Can be plain strings, or functions to be applied
712+
to the (invalid) option argument to produce a string.
711713
712714
parse-opts returns a map with four entries:
713715

src/test/clojure/clojure/tools/cli_test.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
(let [specs (compile-option-specs
122122
[["-p" "--port NUMBER"
123123
:parse-fn parse-int
124-
:validate [#(< 0 % 0x10000) "Must be between 0 and 65536"]]
124+
:validate [#(< 0 % 0x10000) #(str % " is not between 0 and 65536")]]
125125
["-f" "--file PATH"
126126
:missing "--file is required"
127127
:validate [#(not= \/ (first %)) "Must be a relative path"
@@ -143,7 +143,7 @@
143143
(peek (parse-option-tokens specs [[:short-opt "-f" "-p"]] :strict true))))
144144
(is (has-error? #"--file is required"
145145
(peek (parse-option-tokens specs []))))
146-
(is (has-error? #"Must be between"
146+
(is (has-error? #"0 is not between"
147147
(peek (parse-option-tokens specs [[:long-opt "--port" "0"]]))))
148148
(is (has-error? #"Error while parsing"
149149
(peek (parse-option-tokens specs [[:long-opt "--port" "FOO"]]))))

0 commit comments

Comments
 (0)