user2947133
user2947133

Reputation: 325

scopt: Is it possible to refactor single Config into smaller Configs?

I have been using scopt with a single case class:

case class ArgsConfig(
  arg1: type1 = value1,
  arg2: type2 = value2,
  ...
)

I now have a large number of possible arguments which makes the class hard to read. The arguments can be grouped logically into smaller groups, for example, arguments dealing with using Spark, etc.

Is it possible to refactor the single Config into smaller Configs to allow handling of a single command line in an equivalent manner to using a single large Config?

Upvotes: 1

Views: 586

Answers (1)

0__
0__

Reputation: 67310

If I understand you correctly, you want to do the following:

case class SubConfig(x: Int = -1, y: String = "")

case class Config(sub: SubConfig = SubConfig(), z: Boolean = false)

val parser = new scopt.OptionParser[Config]("Foo") {
  opt[Int   ]('x', "ex" ) action { (v, c) => c.copy(sub = c.sub.copy(x = v)) }
  opt[String]('y', "why") action { (v, c) => c.copy(sub = c.sub.copy(y = v)) }
  opt[Unit  ]('z', "zet") action { (v, c) => c.copy(z = true) }
}
parser.parse(args, Config()) // ...

The only annoyance is that you need a nested copy, unless you use a "Lens" library or the mutable parser.

Upvotes: 1

Related Questions