File: 2.0.0.markdown

package info (click to toggle)
scopt 3.5.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 312 kB
  • sloc: xml: 45; makefile: 13
file content (22 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
## immutable parser

In addition to the mutable parser from scopt 1.x, 2.0.0 adds an immutable parser.

    val parser = new scopt.immutable.OptionParser[Config]("scopt", "2.x") { def options = Seq(
      intOpt("f", "foo", "foo is an integer property") { (v: Int, c: Config) => c.copy(foo = v) },
      opt("o", "output", "output") { (v: String, c: Config) => c.copy(bar = v) },
      booleanOpt("xyz", "xyz is a boolean property") { (v: Boolean, c: Config) => c.copy(xyz = v) },
      keyValueOpt("l", "lib", "<libname>", "<filename>", "load library <libname>")
        { (key: String, value: String, c: Config) => c.copy(libname = key, libfile = value) },
      keyIntValueOpt(None, "max", "<libname>", "<max>", "maximum count for <libname>")
        { (key: String, value: Int, c: Config) => c.copy(maxlibname = key, maxcount = value) },
      arg("<file>", "some argument") { (v: String, c: Config) => c.copy(whatnot = v) }
    ) }
    // parser.parse returns Option[C]
    parser.parse(args, Config()) map { config =>
      // do stuff
    } getOrElse {
      // arguments are bad, usage message will have been displayed
    }

Instead of updating the config object in-place, the immutable parser expects call back functions that takes a config object and returns a new one.