OParser
A monadic commandline options parser.
{{{
import scopt.OParser
val builder = OParser.builder[Config]
val parser1 = {
import builder._
OParser.sequence(
programName("scopt"),
head("scopt", "4.x"),
optInt
.action((x, c) => c.copy(foo = x))
.text("foo is an integer property"),
optFile
.required()
.valueName("")
.action((x, c) => c.copy(out = x))
.text("out is a required file property"),
opt(String, Int)
.action({ case ((k, v), c) => c.copy(libName = k, maxCount = v) })
.validate(x =>
if (x.2 > 0) success
else failure("Value must be >0"))
.keyValueName("", "")
.text("maximum count for "),
optSeq[File]
.valueName(",...")
.action((x, c) => c.copy(jars = x))
.text("jars to include"),
optMap[String, String]
.valueName("k1=v1,k2=v2...")
.action((x, c) => c.copy(kwargs = x))
.text("other arguments"),
optUnit
.action((, c) => c.copy(verbose = true))
.text("verbose is a flag"),
optUnit
.hidden()
.action((, c) => c.copy(debug = true))
.text("this option is hidden in the usage text"),
help("help").text("prints this usage text"),
argFile
.unbounded()
.optional()
.action((x, c) => c.copy(files = c.files :+ x))
.text("optional unbounded args"),
note("some notes." + sys.props("line.separator")),
cmd("update")
.action((, c) => c.copy(mode = "update"))
.text("update is a command.")
.children(
optUnit
.abbr("nk")
.action((, c) => c.copy(keepalive = false))
.text("disable keepalive"),
optBoolean
.action((x, c) => c.copy(xyz = x))
.text("xyz is a boolean property"),
optUnit
.hidden()
.action((, c) => c.copy(debug = true))
.text("this option is hidden in the usage text"),
checkConfig(
c =>
if (c.keepalive && c.xyz) failure("xyz cannot keep alive")
else success)
)
)
}
{{{
import scopt.OParser
val builder = OParser.builder[Config]
val parser1 = {
import builder._
OParser.sequence(
programName("scopt"),
head("scopt", "4.x"),
optInt
.action((x, c) => c.copy(foo = x))
.text("foo is an integer property"),
optFile
.required()
.valueName("")
.action((x, c) => c.copy(out = x))
.text("out is a required file property"),
opt(String, Int)
.action({ case ((k, v), c) => c.copy(libName = k, maxCount = v) })
.validate(x =>
if (x.2 > 0) success
else failure("Value must be >0"))
.keyValueName("", "")
.text("maximum count for "),
optSeq[File]
.valueName(",...")
.action((x, c) => c.copy(jars = x))
.text("jars to include"),
optMap[String, String]
.valueName("k1=v1,k2=v2...")
.action((x, c) => c.copy(kwargs = x))
.text("other arguments"),
optUnit
.action((, c) => c.copy(verbose = true))
.text("verbose is a flag"),
optUnit
.hidden()
.action((, c) => c.copy(debug = true))
.text("this option is hidden in the usage text"),
help("help").text("prints this usage text"),
argFile
.unbounded()
.optional()
.action((x, c) => c.copy(files = c.files :+ x))
.text("optional unbounded args"),
note("some notes." + sys.props("line.separator")),
cmd("update")
.action((, c) => c.copy(mode = "update"))
.text("update is a command.")
.children(
optUnit
.abbr("nk")
.action((, c) => c.copy(keepalive = false))
.text("disable keepalive"),
optBoolean
.action((x, c) => c.copy(xyz = x))
.text("xyz is a boolean property"),
optUnit
.hidden()
.action((, c) => c.copy(debug = true))
.text("this option is hidden in the usage text"),
checkConfig(
c =>
if (c.keepalive && c.xyz) failure("xyz cannot keep alive")
else success)
)
)
}
// OParser.parse returns Option[Config]
OParser.parse(parser1, args, Config()) match {
case Some(config) =>
// do something
case _ =>
// arguments are bad, error message will have been displayed
}
OParser.parse(parser1, args, Config()) match {
case Some(config) =>
// do something
case _ =>
// arguments are bad, error message will have been displayed
}
// alternatively, use OParser.runParser returns (Option[Config]
, List[OEffect]
)
OParser.runParser(parser1, args, Config()) match {
case (result, effects) =>
OParser.runEffects(effects, new DefaultOEffectSetup {
// override def displayToOut(msg: String): Unit = Console.out.println(msg)
// override def displayToErr(msg: String): Unit = Console.err.println(msg)
// override def reportError(msg: String): Unit = displayToErr("Error: " + msg)
// override def reportWarning(msg: String): Unit = displayToErr("Warning: " + msg)
OParser.runParser(parser1, args, Config()) match {
case (result, effects) =>
OParser.runEffects(effects, new DefaultOEffectSetup {
// override def displayToOut(msg: String): Unit = Console.out.println(msg)
// override def displayToErr(msg: String): Unit = Console.err.println(msg)
// override def reportError(msg: String): Unit = displayToErr("Error: " + msg)
// override def reportWarning(msg: String): Unit = displayToErr("Warning: " + msg)
// ignore terminate
override def terminate(exitState: Either[String, Unit]): Unit = ()
})
result match {
Some(config) =>
// do something
case _ =>
// arguments are bad, error message will have been displayed
}
}
}}}
Some(config) =>
// do something
case _ =>
// arguments are bad, error message will have been displayed
}
}
}}}
- Companion
- object
class Object
trait Matchable
class Any