以下使用scopt:
import java.io.File
object Application extends App {
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) => println(config)
case _ => println("config undefined")
}
}
我收到编译错误:
[error] /Users/jamesclark/code/scratch/src/main/scala/Application.scala:13: not found: value x
[error] c.copy(out = x)
如果我重命名Config
参数scripts
或val scripts
,则会进行编译。
任何人都可以告诉我这里发生了什么? 这是一个编译器问题,还是我错过了一些魔术?
scala 2.11.8 / sbt 0.13.7 / scopt 3.5.0
答案 0 :(得分:3)
将parser
,config
和scripts
纳入lazy val
而不是val
s会产生更有用的错误消息:/src/main/scala/Application.scala:23: variable definition needs type because 'scripts' is used as a named argument in its body.
为脚本提供类型注释val scripts: Option[Seq[File]] = ...
可以解决问题。
解决此问题的其他方法是重命名val scripts
或case class Config(... scripts: Seq[File] ...)
scripts
次出现。
问题的根源似乎来自scopt库,因为将parser
的定义移动到单独的范围不需要任何重命名或类型注释工作。
object Application extends App {
def newParser(): OptionParser[Config] = {
new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
}
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = newParser()
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) =>
println(config)
case _ =>
println("config undefined")
}
}