在Scala中扩展Scopt OptionParser

时间:2015-12-01 17:57:05

标签: scala inheritance optionparser scopt

我尝试使用带有一些默认参数的基本选项解析器。

在其他项目中,我想用其他参数扩展选项解析器。

类似的东西:

case class Config(foo: String = null)

trait DefaultParser { self: OptionParser[Config] =>
  opt[String]('f', "foo") action { (x, c) =>
    c.copy(foo = x)
  }
}

case class MyConfig(bar: String = "hello world")

trait MyParser { self: OptionParser[MyConfig] =>
  opt[String]('b', "bar") action { (x, c) =>
    c.copy(bar = x)
  }
}

我是Scala的新手,我不确定现在如何在同一args上同时使用它们。

我使用Scala 2.10与scopt_2.10 v3.3.0。

1 个答案:

答案 0 :(得分:2)

我已经开了https://github.com/scopt/scopt/issues/132

到目前为止,我能够提出的最好结果是两个解析器。

case class OutputOpts(
  val outputOption: Int = 1
)

trait OptsWithOutput {
  def output: OutputOpts
}

此解析器位于父类中。

def parseOutputOpts(args: Array[String]): OutputOpts = {
  val parser = new scopt.OptionParser[OutputOpts]("scopt") {
    override def errorOnUnknownArgument = false

    opt[Int]("outputOption") action { (x, c) =>
      c.copy(outputOption = x)
    } text ("some output option")
  }

  parser.parse(args, OutputOpts())
    .getOrElse(throw new Exception("Error parsing output cli args"))
}

儿童班现在可以使用:

case class ChildOpts(
  childThing: Int = 42,
  output: OutputOpts = OutputOpts()
) extends OptsWithOutput

它的解析器结合了两者。

val opts = ChildOpts(output = super.parseOutputOpts(args))

val parser = new scopt.OptionParser[ChildOpts]("scopt") {
  override def errorOnUnknownArgument = false

  opt[Int]("childThing") action { (x, c) =>
    c.copy(childThing = x)
  } text ("some child thing")
}

parser.parse(args, opts).getOrElse(throw new Exception("failed"))

请注意我们必须将errorOnUnknownArgument设置为false,这绝对不理想并会导致警告。