My CLI tool has options, for example -a
-b
and -c
.
If a user passes -d
, I want to fail with an error message. Of course I could code up an ad-hoc check, but how do I do this with tools.cli ?
Errors are communicated in the result of parse-opts
. This means, it's your responsibility to actually fail in the case, you don't accept arbitrary arguments.
Eg
(require '[clojure.tools.cli :refer [parse-opts]])
(def cli-options
[["-a" "--aye" "A"]
["-b" "--bee" "B"]])
(parse-opts *command-line-args* cli-options)
Running with (for the sake of brevity, I use babashka, formatting is mine):
% bb x.bb -a -b -d
{:options {:aye true,
:bee true},
:arguments [],
:summary " -a, --aye A\n -b, --bee B",
:errors ["Unknown option: \"-d\""]}
As you can see, there is a key :errors
and it points to a vector of strings with all the errors.
If you need inspiration for how to deal with the common case of writing a "main" function, that behaves properly (eg show the usage, fail on error...) I suggest a peek at the "Example Usage" section of tools.cli
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.