Skip to content
Rogach edited this page May 26, 2012 · 3 revisions

ScallopConf uses the Scallop builder option under the hood.

The builder is a bit more crumbesome to use, requires more boilerplate, but is completely immutable and more flexible - for example, you can first define your options and add defferent sets of arguments afterwards (can be also handy if you want to delegate some option definitions to sub-modules).

The example usage of the builder:

import org.rogach.scallop._;

val opts = Scallop(List("-d","--num-limbs","1"))
  .version("test 1.2.3 (c) 2012 Mr Placeholder")
  .banner("""Usage: test [OPTION]... [pet-name]
            |test is an awesome program, which does something funny      
            |Options:
            |""".stripMargin)
  .footer("\nFor all other tricks, consult the documentation!")
  .opt[Boolean]("donkey", descr = "use donkey mode")
  .opt("monkeys", default = Some(2), short = 'm')
  .opt[Int]("num-limbs", 'k', 
    "number of libms", required = true)
  .opt[List[Double]]("params")
  .opt[String]("debug", hidden = true)
  .props[String]('D',"some key-value pairs")
   // you can add parameters a bit later
  .args(List("-Dalpha=1","-D","betta=2","gamma=3", "Pigeon"))
  .trailArg[String]("pet name")
  .verify
  
opts.get[Boolean]("donkey") should equal (Some(true))
opts[Int]("monkeys") should equal (2)
opts[Int]("num-limbs") should equal (1)
opts.prop[String]('D',"alpha") should equal (Some("1"))
opts[String]("pet name") should equal ("Pigeon")
intercept[WrongTypeRequest] {
  opts[Double]("monkeys") // this will throw an exception at runtime
                          // because the wrong type is requested
}

println(opts.help) // returns options description

println(opts.summary) // returns summary of parser status