Skip to content

Commit

Permalink
Handling help and version options via catching Scallop's Help and Ver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
Gerd Reiss committed Jan 27, 2017
1 parent b73f62b commit 23b18d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
16 changes: 14 additions & 2 deletions base/src/main/scala/co/uproot/abandon/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.time.format.DateTimeParseException

import com.typesafe.config.{Config, ConfigException, ConfigFactory}
import org.rogach.scallop.ScallopConf
import org.rogach.scallop.exceptions.ScallopException
import org.rogach.scallop.exceptions.{Help, ScallopException, Version}

import scala.collection.JavaConverters._

Expand All @@ -18,7 +18,19 @@ class AbandonCLIConf(arguments: Seq[String]) extends ScallopConf(arguments) {
override def onError(e: Throwable): Unit = e match {
case ex: ScallopException => {
printHelp
throw ex
throw e
}
case Help("") => {
builder.printHelp
throw e
}
case Help(subname) => {
builder.findSubbuilder(subname).get.printHelp
throw e
}
case Version => {
builder.vers.foreach(println)
throw e
}
case other => super.onError(other)
}
Expand Down
12 changes: 7 additions & 5 deletions cli/src/main/scala/co/uproot/abandon/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.FileWriter
import java.nio.file.{Files, Paths}

import co.uproot.abandon.Helper.maxElseZero
import org.rogach.scallop.exceptions.ScallopException
import org.rogach.scallop.exceptions.{Help, ScallopException, Version}

final class ReportWriter(settings: Settings, outFiles: Seq[String]) {
val writesToScreen = outFiles.contains("-") || outFiles.isEmpty
Expand Down Expand Up @@ -255,12 +255,14 @@ object CLIApp {
*/
def run(args: Array[String]): Unit = {
val cliConf = new AbandonCLIConf(args)
cliConf.verify()
cliConf.version("Version: " + CliBuildInfo.version + " [" + CliBuildInfo.builtAtString + "]")

if (cliConf.version.supplied) {
println("Version: " + CliBuildInfo.version + " [" + CliBuildInfo.builtAtString + "]")
} else {
try {
cliConf.verify()
runApp(cliConf)
} catch {
case Help(_) =>
case Version =>
}
}

Expand Down
14 changes: 5 additions & 9 deletions cli/src/test/scala/co/uproot/abandon/CLIMainTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ class CLIMainTest extends FlatSpec with Matchers {
it should "handle version" in {
co.uproot.abandon.CLIApp.run(Array("--version"))
}
it should "handle -h by throwing a Help exception" in {
an [org.rogach.scallop.exceptions.Help] should be thrownBy {
co.uproot.abandon.CLIApp.run(Array("-h"))
}
it should "handle h" in {
co.uproot.abandon.CLIApp.run(Array("-h"))
}
it should "handle --help by throwing a Help exception" in {
an [org.rogach.scallop.exceptions.Help] should be thrownBy {
co.uproot.abandon.CLIApp.run(Array("--help"))
}
it should "handle help" in {
co.uproot.abandon.CLIApp.run(Array("--help"))
}
it should "handle an unknown option by throwing an UnknownOption exception" in {
an [org.rogach.scallop.exceptions.UnknownOption] should be thrownBy {
an[org.rogach.scallop.exceptions.UnknownOption] should be thrownBy {
co.uproot.abandon.CLIApp.run(Array("--unknown"))
}
}
Expand Down

0 comments on commit 23b18d9

Please sign in to comment.