使用 scopt https://github.com/scopt/scopt
我有一个非常简单的 Scala CLI 驱动程序,它在 .parse 的第一行出错。该行是var i = 0,无法想象为什么会失败,也许是我如何实例化OptionParser?
def parse(args: Seq[String], init: C): Option[C] = {
var i = 0 <———————————————— prints the error below
val pendingOptions = ListBuffer() ++ (nonArgs filterNot {_.hasParent})
Exception in thread "main" java.lang.NoSuchMethodError: scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;
at scopt.OptionParser.parse(options.scala:306)
at org.apache.mahout.drivers.ItemSimilarityDriver$.main(ItemSimilarityDriver.scala:47)
at org.apache.mahout.drivers.ItemSimilarityDriver.main(ItemSimilarityDriver.scala)
完整的代码在这里,对不起,但我是 Scala 的新手,所以这可能是一个非常愚蠢的问题
object ItemSimilarityDriver {
/**
* @param args Command line args, if empty a help message is printed.
* @return
*/
def main(args: Array[String]): Unit = {
val parser = new OptionParser[Config]("ItemSimilarity") {
head("ItemSimilarity", "Spark")
opt[Unit]('r', "recursive") action { (_, c) =>
c.copy(recursive = true) } text("The input path should be searched recursively for files that match the filename pattern (optional), Default: false.")
opt[String]('o', "output") required() action { (x, c) =>
c.copy(output = x) } text("Output is a path for all output (required)")
opt[String]('i', "input") required() action { (x, c) =>
c.copy(input = x) } text("Input is a path for input, it may be a filename or dir name. If a directory it will be searched for files matching the -p pattern. (required)")
note("some notes.\n")
help("help") text("prints this usage text")
}
// parser.parse returns Option[C]
parser.parse(args, Config()) map { config => <—————————— parser was created
but this call fails in the parse
// do stuff
//val didIt = true
} getOrElse {
// arguments are bad, error message will have been displayed, throw exception, run away!
}
}
case class Config(recursive: Boolean = false, input: String = null, output: String = null)
}
我也尝试过同样错误的可变选项方法。