使用Scala选项验证命令行参数



我在Scala实用程序应用程序中使用Apache commons CLI进行命令行解析。其中一个参数是数据库端口号(--port=),它覆盖了默认的"5432"(对于PostgreSQL)。我正试图使用Option类来协助验证。这是我想出来的代码。有没有更好的验证方法?

val port = Option(commandLine.getOptionValue("port", "5432")) map {
  try {
    val value = Integer.parseInt(_)
    if (value < 1 || value > 65535) throw new NumberFormatException
    value
  } catch {
    case ex: NumberFormatException =>
      throw new
        IllegalArgumentException("the port argument must be a number between 1 and 65535")
  }
} get

端口号必须为1 ~ 65535之间的整数。

这样做会更好吗?为什么或者为什么不呢?

val port = Option(commandLine.getOptionValue("port")) map {
  try {
    val value = Integer.parseInt(_)
    if (value < 1 || value > 65535) throw new NumberFormatException
    value
  } catch {
    case ex: NumberFormatException =>
      throw new
        IllegalArgumentException("the port argument must be a number between 1 and 65535")
  }
} getOrElse(5432)

我承认我不是100%确定,如果出现问题,您想要抛出什么,或者5432是每个错误值的默认端口,但这是我要做的:

def getPort(candidate: Option[String]) = candidate
   .map { _.toInt } // throws NumberFormatException
   .filter { v => v > 0 && v <= 65535 } // returns Option[Int]
   .getOrElse { throw new IllegalArgumentException("error message") } // return an Int or throws an exception

我想这是我探索验证的好时机。

import scalaz._
import Scalaz._
val port = {
  def checkRange(port: Int): Validation[String, Int] = {
    if (port < 1 || port > 65535) "port not in [1-65535]".fail
    else port.success
  }
  commandLine.getOptionValue("port", "5432")
    .parseInt.flatMap(checkRange) match {
    case Failure(e) => throw new IllegalArgumentException(e.toString)
    case Success(port) => port
  }
}

最新更新