使用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) }
我也尝试过具有相同错误的可变选项方法.
这个问题似乎与Scala库版本和scopt不匹配.目前稳定的scopt 3.2.0交叉发布:
Scala 2.9.1
Scala 2.9.2
Scala 2.9.3
斯卡拉2.10
斯卡拉2.11
Scala 2.10和2.11工件使用sbt 0.12的交叉版本控制约定并使用_2.10
后缀,因为Scala 2.10.x次要版本与Scala 2.10.0二进制兼容.换句话说scopt_2.11
,不是以后的版本scopt_2.10
.一个针对Scala 2.11.x编译,而另一个针对Scala 2.10.x编译.
我建议您尝试管理外部库.sbt有一个插件可以为你生成IntelliJ项目.