I opened https://github.com/scopt/scopt/issues/132 .
So far, the best I've been able to come up with is combining two parsers.
case class OutputOpts(
val outputOption: Int = 1
)
trait OptsWithOutput {
def output: OutputOpts
}
The parser for this lives in the parent class.
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"))
}
Now you can use the child class:
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, .