scala case class Config(foo: Int = -1, bar: String = "") val parser = new OptionParser[Config]("scopt-example") { opt[Int]("foo") .action((x, c) => c.copy(foo = x)) .text("foo is an integer property") opt[String]("bar") .action((x, c) => c.copy(bar = x)) .text("bar is a string property") } parser.parse(args, Config()) match { case Some(config) => case None => } scala opt[Int]("foo") .validate(x => if (x > 0) success else failure("Option --foo must be greater than 0")) .action((x, c) => c.copy(foo = x)) .text("foo is an integer property") scala sealed trait Command case class FooConfig(foo: Int = -1) extends Command case class BarConfig(bar: String = "") extends Command val parser = new OptionParser[Command]("scopt-example") { cmd("foo") .action((_, c) => FooConfig()) .text("run foo command") cmd("bar") .action((_, c) => BarConfig()) .text("run bar command") .children( opt[String]("bar") .action((x, c) => c.copy(bar = x)) .text("bar is a string property") ) } parser.parse(args, Command()) match { case Some(FooConfig(foo)) => case Some(BarConfig(bar)) => case None => }


上一篇:
下一篇:
切换中文