On Wed 22 Jan 2014 at 01:48:22PM +0100, Alf Kristian Støyle wrote: > From the documentation it seems that specifying the second part of the > "long option" should be enough to make an argument required. So this > minimal example should also produce errors: > > (def cli-options-simplified > [["-p" "--port PORT" "Port number"]]) > ;=> (var user/cli-options-simplified) > > (:errors (parse-opts [] cli-options-simplified)) > ;=> nil > > So what are we misunderstanding? Any pointers would be greatly appreciated. > > Using Clojure 1.5.1 with tools.cli 0.3.1.
Hi Alf,
An option with a required argument (via "--port PORT" or the :required
entry) only specifies that the option must be followed by an argument
_if_ it is specified on the command line. For example,
$ cmd --port
is an error, but simply
$ cmd
is not.
If you would like require that --port must be specified in every
invocation, you can assert its existence/value in the options map:
(let [{:keys [options errors …]} (parse-opts argv cli-options)]
…
(assert (:port options) "`--port` must be specified")
…)
You can, of course, set a :default value for a required option.
HTH
guns
pgpBXXVLuJtaf.pgp
Description: PGP signature
