Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created December 3, 2017 06:01
Show Gist options
  • Save n4to4/970021533d2db36c96bf1850756a5e9c to your computer and use it in GitHub Desktop.
Save n4to4/970021533d2db36c96bf1850756a5e9c to your computer and use it in GitHub Desktop.
scopt.scala
object Main extends App {
import java.io.File
case class Config(
foo: Int = -1,
//out: File = new File("."),
// xyz: Boolean = false,
// libName: String = "",
// maxCount: Int = -1,
// mode: String = "",
// files: Seq[File] = Seq(),
// keepalive: Boolean = false,
// jars: Seq[File] = Seq(),
// kwargs: Map[String, String] = Map(),
// debug: Boolean = false,
verbose: Boolean = false
)
val parser = new scopt.OptionParser[Config]("scopt") {
head("scopt", "3.x")
opt[Unit]("verbose").action( (_, c) =>
c.copy(verbose = true)
).text("verbose is a flag")
opt[Int]('f', "foo").action( (x, c) =>
c.copy(foo = x) ).text("foo is an integer property")
// opt[File]('o', "out").required().valueName("<file>").
// action( (x, c) => c.copy(out = x) ).
// text("out is a required file property")
// opt[(String, Int)]("max").action({
// case ((k, v), c) => c.copy(libName = k, maxCount = v) }).
// validate( x =>
// if (x._2 > 0) success
// else failure("Value <max> must be >0") ).
// keyValueName("<libname>", "<max>").
// text("maximum count for <libname>")
// opt[Seq[File]]('j', "jars").valueName("<jar1>,<jar2>...").action( (x,c) =>
// c.copy(jars = x) ).text("jars to include")
// opt[Map[String,String]]("kwargs").valueName("k1=v1,k2=v2...").action( (x, c) =>
// c.copy(kwargs = x) ).text("other arguments")
// opt[Unit]("debug").hidden().action( (_, c) =>
// c.copy(debug = true) ).text("this option is hidden in the usage text")
// help("help").text("prints this usage text")
// arg[File]("<file>...").unbounded().optional().action( (x, c) =>
// c.copy(files = c.files :+ x) ).text("optional unbounded args")
// note("some notes.".newline)
// cmd("update").action( (_, c) => c.copy(mode = "update") ).
// text("update is a command.").
// children(
// opt[Unit]("not-keepalive").abbr("nk").action( (_, c) =>
// c.copy(keepalive = false) ).text("disable keepalive"),
// opt[Boolean]("xyz").action( (x, c) =>
// c.copy(xyz = x) ).text("xyz is a boolean property"),
// opt[Unit]("debug-update").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 )
// )
}
parser.parse(args, Config()) match {
case None =>
case Some(config) =>
println(config)
}
trait Parse[Conf] { self =>
type Value
val shortName: Char
val longName: String
val text: String
import scopt._
final def opt(parser: OptionParser[Conf])(implicit ev: Read[Value]): OptionDef[Value, Conf] =
parser.opt[Value](shortName, longName).action((x, c) =>
self.action(x, c)
).text(self.text)
def action(x: Value, config: Conf): Conf
}
val verboseParser = new Parse[Config] {
type Value = Unit
val longName = "verbose"
val shortName = 'v'
val text = ""
def action(x: Unit, config: Config): Config = config.copy(verbose = true)
}
val intParser = new Parse[Config] {
type Value = Int
val longName = "foo"
val shortName = 'f'
val text = "foo is an integer property"
def action(f: Int, config: Config): Config = config.copy(foo = f)
}
val p = new scopt.OptionParser[Config]("scopt") {
verboseParser.opt(this)
intParser.opt(this)
}
val out = p.parse(args, Config()) match {
case None =>
case Some(config) =>
println(config)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment