clojure/tools.cli — Issue #2: Interactions between :required true and other options seems a bit off
Author: garypen
Created: 2011-11-03 13:21:51+00:00
State: open

If you set an option to be :required true, then you must also set a :default value, even if there is no reasonable default, or you get an exception.

Here's my code:

(cli args
["-e" "--examiner" "Email validator"]
["-h" "--help" "Display help" :default false :flag true]
["-o" "--originator" "The originator" :required true]
["-s" "--server" "Email server" :required true]
["-t" "--tester" "Testing address"])

There is no reasonable default for originator or server, but if I run the -h (for help) command, then I get an exception:

bash-4.1$ lein run -h
Exception in thread "main" java.lang.RuntimeException: java.lang.Exception: :server is a required argument
at clojure.lang.Util.runtimeException(Util.java:165)
at clojure.lang.Compiler.eval(Compiler.java:6476)
at clojure.lang.Compiler.eval(Compiler.java:6455)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at clojure.main$eval_opt.invoke(main.clj:296)
at clojure.main$initialize.invoke(main.clj:315)
at clojure.main$null_opt.invoke(main.clj:348)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: :server is a required argument
at clojure.tools.cli$ensure_required_provided.invoke(cli.clj:115)
at clojure.tools.cli$cli.doInvoke(cli.clj:122)
at clojure.lang.RestFn.invoke(RestFn.java:516)
at secret_santa.core$_main.doInvoke(core.clj:71)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:401)
at user$eval62.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6465)
... 12 more

Since -h is (almost universally) a help option, I wouldn't expect to have to provide required values for other arguments when I ran the -h option.

There is a work-around. If I specify a default (it's ok to specify an empty string "" for these values), then I get the behavior I would expect.

Two proper solutions leap to mind. Firstly, check for the existence of a -h flag and if that is provided don't bother enforcing required options. That's a bit nasty, so perhaps a new option type would be better to indicate that the presence of a particular option suspends the need to enforce required options.

e.g.:

:no-required option. Default false, if set on any option, then :required is suspended for all other options.

(cli args
["-e" "--examiner" "Email validator"]
["-h" "--help" "Display help" :default false :flag true :no-required true]
["-o" "--originator" "The originator" :required true]
["-s" "--server" "Email server" :required true]
["-t" "--tester" "Testing address"])

Or maybe a better solution would be a flag to the cli function which suspends :required?