case-app
case-app copied to clipboard
Trying to get desired behavior
Here is a short test program:
import caseapp._
case class EwApiDemoOptions (
@ExtraName("l") displaySpeciesList: Boolean = false,
@ExtraName("s") displaySpecies: String = ""
) extends App {
if (displaySpeciesList) {
println("speciesList")
} else if (displaySpecies.nonEmpty) {
println("displaySpecies")
} else if (!displaySpeciesList && displaySpecies.isEmpty)
Console.err.println(CaseApp.helpMessage[EwApiDemoOptions])
}
object EwApiDemo extends AppOf[EwApiDemoOptions]
- Calling with the -Xlint option enables the check for
delayedinit-select, which causes this warning to appear several times:Selecting value displaySpeciesList from class EwApiDemoOptions, which extends scala.DelayedInit, is likely to yield an uninitialized value. Yes, I can specify:
scalacOptions ++= Seq(
"-Xlint",
"-Xlint:-delayedinit-select"
)
But that turns off the warnings for any other delayed init issues that may exist. Is there another way to structure the program to avoid delayed initialization entirely?
- Invoking a option that requires an argument, but failing to provide a value for the argument, should cause an error message to be generated and the program to stop. Instead, the feeble
argument missingmessage is displayed and then an ugly exception is thrown:
$ sbt "runMain EwApiDemo2 -s"
... lots of output ...
argument missing
Exception: sbt.TrapExitSecurityException thrown from the UncaughtExceptionHandler in thread "run-main-0"
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:runMain for the full output.
[error] (compile:runMain) Nonzero exit code: 1
[error] Total time: 1 s, completed Mar 1, 2017 5:43:30 PM
Users should not see the exception. Instead, they should politely be told what happened, and what they need to do to correct the problem. A better output would be:
Error: the -s/--displaySpecies option requires an argument, but none was supplied. Please retry with a value for the option.
Until this is fixed, how might I catch the exception?