【问题标题】:Scala option parsing with scopt使用 scopt 解析 Scala 选项
【发布时间】:2014-05-02 18:10:22
【问题描述】:

使用 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)

}

我也尝试了可变选项方法,但出现了同样的错误。

【问题讨论】:

  • 错误是java.lang.NoSuchMethodError。您使用的是什么构建工具?您是否包含了与 scopt 匹配的 Scala 库版本?
  • 在 Intellij 中运行,它使用 pom 添加库,使用 Scala 2.10.3,这似乎在 scopt 的 build.sbt 中得到支持。我的项目中有 scala-library-2.10.3.jar。
  • 如果我回到 scopt 2.10 似乎可以工作,即 maven 神器。我去看看能不能升级一下项目,不过太大了

标签: scala scopt


【解决方案1】:

问题似乎是 Scala 库版本和 scopt 不匹配。当前稳定的 scopt 3.2.0 交叉发布:

  • Scala 2.9.1
  • Scala 2.9.2
  • Scala 2.9.3
  • Scala 2.10
  • Scala 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 来管理外部库。 sbt 有一个插件可以为你生成 IntelliJ 项目。

【讨论】:

  • 我一定是误读了 build.sbt 并且认为它可以与 2.10.3 一起使用 正如我所说的,早期版本的 scopt for 2.10 可以工作。我意识到所有新语言及其工具都比其他语言更好;-) 这只是需要时间。顺便说一句,我讨厌 Maven!
  • 你好,哪个版本的 scala 和哪个版本的 scopt 可以完美运行?我使用 scalaVersion := "2.11.4" 和 "com.github.scopt" %% "scopt" % "3.2.0",它们不匹配。
  • @cindywmiao,你找到 scala 和 scopt 的有效组合了吗?
  • @dmreshet 是版本问题,我必须使用 scala 2.10 和 scopt 3.2,我找不到其他解决方法。
  • scopt_2.11 - 3.5.0 和 scala - 2.11.8 适合我。
猜你喜欢
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多