scopt3 示例脚本无法编译
scopt3 sample script does not compile
我正在尝试在我的项目中使用 scopt3,但即使对于 scopt3 Github 页面上的示例代码,我也会遇到编译错误:
val parser = new scopt.OptionParser[Config]("scopt") {
head("scopt", "3.x")
opt[Int]('f', "foo") action { (x, c) =>
c.copy(foo = x) } text("foo is an integer property")
opt[File]('o', "out") required() valueName("<file>") action { (x, c) =>
c.copy(out = x) } text("out is a required file property")
opt[(String, Int)]("max") action { case ((k, v), c) =>
c.copy(libName = k, maxCount = v) } validate { x =>
if (x._2 > 0) success else failure("Value <max> must be >0")
} keyValueName("<libname>", "<max>") text("maximum count for <libname>")
opt[Seq[File]]('j', "jars") valueName("<jar1>,<jar2>...") action { (x,c) =>
c.copy(jars = x) } text("jars to include")
opt[Map[String,String]]("kwargs") valueName("k1=v1,k2=v2...") action { (x, c) =>
c.copy(kwargs = x) } text("other arguments")
opt[Unit]("verbose") action { (_, c) =>
c.copy(verbose = true) } text("verbose is a flag")
opt[Unit]("debug") hidden() action { (_, c) =>
c.copy(debug = true) } text("this option is hidden in the usage text")
note("some notes.\n")
help("help") text("prints this usage text")
arg[File]("<file>...") unbounded() optional() action { (x, c) =>
c.copy(files = c.files :+ x) } text("optional unbounded args")
cmd("update") action { (_, c) =>
c.copy(mode = "update") } text("update is a command.") children(
opt[Unit]("not-keepalive") abbr("nk") action { (_, c) =>
c.copy(keepalive = false) } text("disable keepalive"),
opt[Boolean]("xyz") action { (x, c) =>
c.copy(xyz = x) } text("xyz is a boolean property"),
checkConfig { c =>
if (c.keepalive && c.xyz) failure("xyz cannot keep alive") else success }
)
}
错误是:
1) 未找到类型 Config
我以为它是 com.typesafe.config.Config,但是当我导入时我得到 "velue copy is not a member of com.typesafe.config.Config"。 Config从哪里来?
2) 没有找到值 foo
.copy() 方法的所有参数都标记为 "not found values"(我想是由于之前的 Config 错误)
我正在使用 scala 2.11.6 / SBT 0.13.8
有什么帮助吗?
scopt 与类型安全配置完全无关。
如果您仔细阅读 README
,您会注意到 Config
是在您粘贴的代码之前定义的:
case class Config(foo: Int = -1, out: File = new File("."), xyz: Boolean = false,
libName: String = "", maxCount: Int = -1, verbose: Boolean = false, debug: Boolean = false,
mode: String = "", files: Seq[File] = Seq(), keepalive: Boolean = false,
jars: Seq[File] = Seq(), kwargs: Map[String,String] = Map())
这只是一个包含程序所有参数的对象的示例。
这个想法是你用你想要的任何配置对象来参数化OptionParser
。例如
case class Foo(name: String)
val parser = new scopt.OptionParser[Foo]("foo") {
opt[String]('n', "name") action { (n, foo) => foo.copy(name = n) }
}
val res = parser.parse(args, Foo("default"))
我正在尝试在我的项目中使用 scopt3,但即使对于 scopt3 Github 页面上的示例代码,我也会遇到编译错误:
val parser = new scopt.OptionParser[Config]("scopt") {
head("scopt", "3.x")
opt[Int]('f', "foo") action { (x, c) =>
c.copy(foo = x) } text("foo is an integer property")
opt[File]('o', "out") required() valueName("<file>") action { (x, c) =>
c.copy(out = x) } text("out is a required file property")
opt[(String, Int)]("max") action { case ((k, v), c) =>
c.copy(libName = k, maxCount = v) } validate { x =>
if (x._2 > 0) success else failure("Value <max> must be >0")
} keyValueName("<libname>", "<max>") text("maximum count for <libname>")
opt[Seq[File]]('j', "jars") valueName("<jar1>,<jar2>...") action { (x,c) =>
c.copy(jars = x) } text("jars to include")
opt[Map[String,String]]("kwargs") valueName("k1=v1,k2=v2...") action { (x, c) =>
c.copy(kwargs = x) } text("other arguments")
opt[Unit]("verbose") action { (_, c) =>
c.copy(verbose = true) } text("verbose is a flag")
opt[Unit]("debug") hidden() action { (_, c) =>
c.copy(debug = true) } text("this option is hidden in the usage text")
note("some notes.\n")
help("help") text("prints this usage text")
arg[File]("<file>...") unbounded() optional() action { (x, c) =>
c.copy(files = c.files :+ x) } text("optional unbounded args")
cmd("update") action { (_, c) =>
c.copy(mode = "update") } text("update is a command.") children(
opt[Unit]("not-keepalive") abbr("nk") action { (_, c) =>
c.copy(keepalive = false) } text("disable keepalive"),
opt[Boolean]("xyz") action { (x, c) =>
c.copy(xyz = x) } text("xyz is a boolean property"),
checkConfig { c =>
if (c.keepalive && c.xyz) failure("xyz cannot keep alive") else success }
)
}
错误是:
1) 未找到类型 Config
我以为它是 com.typesafe.config.Config,但是当我导入时我得到 "velue copy is not a member of com.typesafe.config.Config"。 Config从哪里来?
2) 没有找到值 foo
.copy() 方法的所有参数都标记为 "not found values"(我想是由于之前的 Config 错误)
我正在使用 scala 2.11.6 / SBT 0.13.8
有什么帮助吗?
scopt 与类型安全配置完全无关。
如果您仔细阅读 README
,您会注意到 Config
是在您粘贴的代码之前定义的:
case class Config(foo: Int = -1, out: File = new File("."), xyz: Boolean = false,
libName: String = "", maxCount: Int = -1, verbose: Boolean = false, debug: Boolean = false,
mode: String = "", files: Seq[File] = Seq(), keepalive: Boolean = false,
jars: Seq[File] = Seq(), kwargs: Map[String,String] = Map())
这只是一个包含程序所有参数的对象的示例。
这个想法是你用你想要的任何配置对象来参数化OptionParser
。例如
case class Foo(name: String)
val parser = new scopt.OptionParser[Foo]("foo") {
opt[String]('n', "name") action { (n, foo) => foo.copy(name = n) }
}
val res = parser.parse(args, Foo("default"))