Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala option parsing with scopt

Tags:

scala

scopt

Using scopt https://github.com/scopt/scopt

I have a very simple Scala CLI driver that errors out on the first line of .parse. The line is var i = 0, can’t imagine why that would fail, maybe in how i instantiated the 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)

Full code here, sorry but I’m new to Scala so this may be a really stupid question

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)

}

I've also tried the mutable options method with the same error.

like image 892
pferrel Avatar asked Mar 19 '23 17:03

pferrel


1 Answers

The problem seems to be mismatch in Scala library version and scopt. The current stable scopt 3.2.0 is cross published against:

  • Scala 2.9.1
  • Scala 2.9.2
  • Scala 2.9.3
  • Scala 2.10
  • Scala 2.11

Scala 2.10 and 2.11 artifacts uses the sbt 0.12's cross versioning convention and uses _2.10 suffix because Scala 2.10.x minor releases are binary compatible with Scala 2.10.0. In other words scopt_2.11 is NOT a later version of scopt_2.10. One is compiled against Scala 2.11.x while the other Scala 2.10.x.

I'd recommend you give sbt a try to manage external libraries. sbt has a plugin to generate IntelliJ project for you.

like image 131
Eugene Yokota Avatar answered Mar 22 '23 07:03

Eugene Yokota