OParser

class OParser[A, C](head: OptionDef[A, C], rest: List[OptionDef[_, C]])

A monadic commandline options parser.

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"),
   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]("verbose")
     .action((_, c) => c.copy(verbose = true))
     .text("verbose is a flag"),
   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." + sys.props("line.separator")),
   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)
     )
 )
}

// 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
}

// 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)

     // 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
   }
}
Companion
object
class Object
trait Matchable
class Any

Value members

Concrete methods

def ++(other: OParser[_, C]): OParser[A, C]
def abbr(x: String): OParser[A, C]

Adds short option -x.

Adds short option -x.

def action(f: (A, C) => C): OParser[A, C]

Adds a callback function.

Adds a callback function.

def children(cs: OParser[_, C]*): OParser[A, C]

Adds a parser under this command.

Adds a parser under this command.

def flatMap(f: Unit => OParser[_, C]): OParser[A, C]
def foreach(f: Unit => Unit): Unit
def hidden(): OParser[A, C]

Hides the option in any usage text.

Hides the option in any usage text.

def keyName(x: String): OParser[A, C]

Adds key name used in the usage text.

Adds key name used in the usage text.

def keyValueName(k: String, v: String): OParser[A, C]

Adds key and value names used in the usage text.

Adds key and value names used in the usage text.

def map(f: Unit => Unit): OParser[A, C]
def maxOccurs(n: Int): OParser[A, C]

Allows the argument to appear at most n times.

Allows the argument to appear at most n times.

def minOccurs(n: Int): OParser[A, C]

Requires the option to appear at least n times.

Requires the option to appear at least n times.

def optional(): OParser[A, C]

Chanages the option to be optional.

Chanages the option to be optional.

def required(): OParser[A, C]

Requires the option to appear at least once.

Requires the option to appear at least once.

def text(x: String): OParser[A, C]

Adds description in the usage text.

Adds description in the usage text.

def toList: List[OptionDef[_, C]]
def unbounded(): OParser[A, C]

Allows the argument to appear multiple times.

Allows the argument to appear multiple times.

def validate(f: A => Either[String, Unit]): OParser[A, C]

Adds custom validation.

Adds custom validation.

def valueName(x: String): OParser[A, C]

Adds value name used in the usage text.

Adds value name used in the usage text.