File tree Expand file tree Collapse file tree
main/clojure/clojure/tools
test/clojure/clojure/tools Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11target
22/.lein-failures
3+ /.lsp
34/.cpcache
45/cljs-test-runner-out
56/.lein-repl-history
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -174,11 +174,13 @@ be useful within your own `:summary-fn` for generating the default summary.
174174
175175There 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
183185If the validation-fn returns a falsey value, the validation-msg is added to the
184186errors vector.
Original file line number Diff line number Diff line change 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
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]
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
Original file line number Diff line number Diff line change 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"
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" ]]))))
You can’t perform that action at this time.
0 commit comments