5

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.

pferrel
  • 5,673
  • 5
  • 30
  • 41
  • The error is `java.lang.NoSuchMethodError`. What build tool are you using? Did you include the version of the Scala library that matches with scopt? – Eugene Yokota May 02 '14 at 18:15
  • running in Intellij, which uses the pom to add libs, using Scala 2.10.3, which seems to be supported in scopt's build.sbt. I have scala-library-2.10.3.jar in my project. – pferrel May 02 '14 at 18:32
  • Seem to work if I go back to scopt 2.10, the maven artifact that is. I'll see if I can upgrade the project, but it's pretty big – pferrel May 02 '14 at 19:17

1 Answers1

7

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.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • I must have misread the build.sbt and thought is would work with 2.10.3 As I said, the earlier version of scopt for 2.10 works. I realize that all new languages and their tools are better than the alternative ;-) It will just take time. BTW I hate maven! – pferrel May 03 '14 at 03:15
  • 1
    hello, which version of scala and which version of scopt will work perfectly? I use scalaVersion := "2.11.4" and , "com.github.scopt" %% "scopt" % "3.2.0", they don't match. – cindywmiao Aug 18 '16 at 07:48
  • @cindywmiao, did you find working combination of scala and scopt? – dmreshet Oct 06 '16 at 20:09
  • @dmreshet it's version problem, I must user scala 2.10 and scopt 3.2, I could not find the other way to fix it. – cindywmiao Nov 22 '16 at 09:47
  • scopt_2.11 - 3.5.0 and scala - 2.11.8 works fine for me. – dmreshet Nov 24 '16 at 12:15