Trait

com.frugalmechanic.optparse

OptParse

Related Doc: package optparse

Permalink

trait OptParse extends OptParseImplicits with OptParseTypes

Simple Command Line Parsing for Scala

OptParse provides simple command line parsing for Scala that only requires a minimal amount of code.

Hello World Example

import com.frugalmechanic.optparse._

object SimpleApp extends OptParse {
  val name = StrOpt()

  def main(args: Array[String]) {
    parse(args)
    println("Hello "+name.getOrElse("world"))
  }
}

And then you can pass options with:

./simpleApp --name World

or

./simpleApp -n World

More Complete Example

import com.frugalmechanic.optparse._

object MyApp extends OptParse {
  // --flag (-f is ambiguous since it overlaps with file)
  val flag = BoolOpt()

  // --name (-n is ambiguous since it overlaps with number)
  val name = StrOpt()

  // Can be called multiple times with --aliases or -a (e.g.: --aliases Foo --aliases Bar OR -a Foo -a Bar)
  val aliases = MultiStrOpt()

  // --number (-n is ambiguous since it overlaps with name)
  val number = IntOpt()

  // --file (-f is ambiguous since it overlaps with flag)
  val file = FileOpt()

  def main(args: Array[String]) {
    // Parse the command line arguments
    parse(args)

    // Implicit conversion to bool
    if (flag) println("flag was set!")

    // Implicit conversion to bool to check if a value is set
    if (name) println("Name: "+name.get)

    if (aliases) println("Your alias(es) are: "+aliases.getOrElse(Nil))

    if (number) println("Your number is: "+number.get)

    if (file) println("Your file is: "+file.get)
  }

}
Show help message
./myApp --help

or

./myApp -h
Pass in some options
./myApp --flag --name Tim --aliases Timothy --aliases Timmy --number 123

Nested Options Object Example

You can also use a nested options object (or class) for parsing the options:

import com.frugalmechanic.optparse._

object MyApp2 {
  object options extends OptParse {
     val flag = BoolOpt()
  }

  def main(args: Array[String]) {
    options.parse(args)

    if (options.flag) println("flag is set")
  }
}

Command Line Option Types

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. OptParse
  2. OptParseTypes
  3. OptParseImplicits
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type ArgOpt[T] = optparse.ArgOpt[T]

    Permalink
    Definition Classes
    OptParseTypes
  2. type BoolOpt = optparse.BoolOpt

    Permalink
    Definition Classes
    OptParseTypes
  3. type FileOpt = optparse.FileOpt

    Permalink
    Definition Classes
    OptParseTypes
  4. type IntOpt = optparse.IntOpt

    Permalink
    Definition Classes
    OptParseTypes
  5. type MultiOpt[T] = optparse.MultiOpt[T]

    Permalink
    Definition Classes
    OptParseTypes
  6. type MultiStrOpt = optparse.MultiStrOpt

    Permalink
    Definition Classes
    OptParseTypes
  7. type Opt = optparse.Opt

    Permalink
    Definition Classes
    OptParseTypes
  8. type OptParse = optparse.OptParse

    Permalink
    Definition Classes
    OptParseTypes
  9. type OptVal[T] = optparse.OptVal[T]

    Permalink
    Definition Classes
    OptParseTypes
  10. type SingleOpt[T] = optparse.SingleOpt[T]

    Permalink
    Definition Classes
    OptParseTypes
  11. type StrOpt = optparse.StrOpt

    Permalink
    Definition Classes
    OptParseTypes

Value Members

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

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

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. val BoolOpt: optparse.BoolOpt.type

    Permalink
    Definition Classes
    OptParseTypes
  5. implicit def BoolOptToBool(opt: optparse.BoolOpt): Boolean

    Permalink

    Allows "if (MyFlag) ..."

    Allows "if (MyFlag) ..."

    Definition Classes
    OptParseImplicits
  6. val FileOpt: optparse.FileOpt.type

    Permalink
    Definition Classes
    OptParseTypes
  7. val IntOpt: optparse.IntOpt.type

    Permalink
    Definition Classes
    OptParseTypes
  8. val MultiStrOpt: optparse.MultiStrOpt.type

    Permalink
    Definition Classes
    OptParseTypes
  9. implicit def OptToBool[T](opt: optparse.OptVal[T]): Boolean

    Permalink

    Allows: if (NameOpt) ...

    Allows: if (NameOpt) ... instead of if (NameOpt.value.isDefined) ...

    Definition Classes
    OptParseImplicits
  10. implicit def OptToSeq[T](opt: optparse.Opt): Seq[optparse.Opt]

    Permalink

    Allows BoolOpt(enables=MyOpt) instead of BoolOpt(enables=Seq(MyOpt))

    Allows BoolOpt(enables=MyOpt) instead of BoolOpt(enables=Seq(MyOpt))

    Definition Classes
    OptParseImplicits
  11. implicit def OptValToOption[T](opt: optparse.OptVal[T]): Option[T]

    Permalink

    Allows any Option methods to be used on an OptVal

    Allows any Option methods to be used on an OptVal

    Definition Classes
    OptParseImplicits
  12. val StrOpt: optparse.StrOpt.type

    Permalink
    Definition Classes
    OptParseTypes
  13. implicit def StringToValidateRegex(regex: Regex): (String) ⇒ Boolean

    Permalink

    Allows regex usage for the validate option: validate="^[a-zA-Z]+$"

    Allows regex usage for the validate option: validate="^[a-zA-Z]+$"

    Definition Classes
    OptParseImplicits
  14. implicit def ValToOption[T](value: T): Option[T]

    Permalink

    Allows longName="name" without needing long=Some("name")

    Allows longName="name" without needing long=Some("name")

    Definition Classes
    OptParseImplicits
  15. val allOpts: ListBuffer[Opt]

    Permalink
    Attributes
    protected
  16. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  17. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @throws( ... )
  18. def defaultOpt[T](arg: ArgOpt[T]): ArgOpt[T]

    Permalink

    Declare a "default option"

  19. final def eq(arg0: AnyRef): Boolean

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

    Permalink
    Definition Classes
    AnyRef → Any
  21. def error(msg: String): Unit

    Permalink

    Exit the application with a message

    Exit the application with a message

    Attributes
    protected
  22. def exit(msg: String = null, status: Int = 1): Nothing

    Permalink
    Attributes
    protected
  23. val foundOpts: ListBuffer[Opt]

    Permalink
    Attributes
    protected
  24. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  25. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  26. def help(): Unit

    Permalink

    Print the help message to STDERR

  27. val helpOpt: optparse.BoolOpt

    Permalink

    A default help option (--help or -h) that displays the help message

  28. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  29. val longNames: HashMap[String, Opt]

    Permalink
    Attributes
    protected
  30. final def ne(arg0: AnyRef): Boolean

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  32. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  33. def optName(opt: Opt): String

    Permalink
    Attributes
    protected
  34. val optParseDebug: Boolean

    Permalink

    Override to true to enable simple println's showing what OptParse is doing

  35. val optParseExitOnError: Boolean

    Permalink

    By default OptParse will do a System.exit on any parsing errors or when --help is invoked.

    By default OptParse will do a System.exit on any parsing errors or when --help is invoked. You can override this to false if you want an IllegalArgumentException exception thrown instead

  36. def parse(args: Array[String]): Unit

    Permalink

    Parse the given command line options

  37. val shortNames: HashMap[Char, Opt]

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

    Permalink
    Definition Classes
    AnyRef
  39. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  40. final def wait(arg0: Long, arg1: Int): Unit

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  42. final def wait(): Unit

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

Deprecated Value Members

  1. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @Deprecated @deprecated @throws( classOf[java.lang.Throwable] )
    Deprecated

    (Since version ) see corresponding Javadoc for more information.

Inherited from OptParseTypes

Inherited from OptParseImplicits

Inherited from AnyRef

Inherited from Any

Ungrouped