Class/Object

scopt

OParser

Related Docs: object OParser | package scopt

Permalink

class OParser[A, C] extends AnyRef

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("")
      .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  must be >0"))
      .keyValueName("", "")
      .text("maximum count for "),
    opt[Seq[File]]('j', "jars")
      .valueName(",...")
      .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]("...")
      .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
    }
}
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. OParser
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new OParser(head: OptionDef[A, C], rest: List[OptionDef[_, C]])

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. def ++(other: OParser[_, C]): OParser[A, C]

    Permalink
  4. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  5. def abbr(x: String): OParser[A, C]

    Permalink

    Adds short option -x.

  6. def action(f: (A, C) ⇒ C): OParser[A, C]

    Permalink

    Adds a callback function.

  7. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  8. def children(cs: OParser[_, C]*): OParser[A, C]

    Permalink

    Adds a parser under this command.

  9. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. def flatMap(f: (Unit) ⇒ OParser[_, C]): OParser[A, C]

    Permalink
  14. def foreach(f: (Unit) ⇒ Unit): Unit

    Permalink
  15. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  16. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  17. def hidden(): OParser[A, C]

    Permalink

    Hides the option in any usage text.

  18. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  19. def keyName(x: String): OParser[A, C]

    Permalink

    Adds key name used in the usage text.

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

    Permalink

    Adds key and value names used in the usage text.

  21. def map(f: (Unit) ⇒ Unit): OParser[A, C]

    Permalink
  22. def maxOccurs(n: Int): OParser[A, C]

    Permalink

    Allows the argument to appear at most n times.

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

    Permalink

    Requires the option to appear at least n times.

  24. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  25. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  26. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  27. def optional(): OParser[A, C]

    Permalink

    Chanages the option to be optional.

  28. def required(): OParser[A, C]

    Permalink

    Requires the option to appear at least once.

  29. def subHead[B](head: OptionDef[B, C]): OParser[B, C]

    Permalink
    Attributes
    protected
  30. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  31. def text(x: String): OParser[A, C]

    Permalink

    Adds description in the usage text.

  32. def toList: List[OptionDef[_, C]]

    Permalink
  33. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  34. def unbounded(): OParser[A, C]

    Permalink

    Allows the argument to appear multiple times.

  35. def validate(f: (A) ⇒ Either[String, Unit]): OParser[A, C]

    Permalink

    Adds custom validation.

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

    Permalink

    Adds value name used in the usage text.

  37. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  38. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  39. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped