加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用scopt解析Scala选项

发布时间:2020-12-16 18:04:49 所属栏目:安全 来源:网络整理
导读:使用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 ———
使用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
> 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有一个插件可以为你生成IntelliJ项目.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读