Package

com.frugalmechanic

optparse

Permalink

package optparse

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

Type Members

  1. abstract class ArgOpt[T] extends OptVal[T]

    Permalink

    Base type for command line options that take an argument

  2. class BoolOpt extends OptVal[Boolean]

    Permalink

    A boolean/flag command line option

    A boolean/flag command line option

    Examples

    import com.frugalmechanic.optparse._
    
    object MyApp extends OptParse {
      // Basic flag using a default long name of --flag and short name of -f
      val flag = BoolOpt()
    
      // Example with all available options instead of relying on the defaults
      val verbose = BoolOpt(
         long="verbose",     // Long name to use (--verbose)
         short="v",          // Short name to use (-v)
         default=false,      // Default value for this flag (true/false)
         desc="Be verbose",  // Help message description
         enables=noisy,      // Other flags to enable if this one is enabled (single option or a Seq of options)
         disables=Seq(quiet),// Other flags to disable if this one is enabled (single option or a Seq of options)
         invalidWith=quiet,  // Other options this flag is invalid with (they cannot be set)
         validWith=noisy,    // Other options that are required with this flag
         exclusive=false,    // Other options can be set when this option is set
      )
    
      // You can also use "new BoolOpt()" instead of using the companion object
      val noisy = new BoolOpt()
      val quiet = new BoolOpt()
    
      def main(args:Array[String]) {
        parse(args)
    
        if (verbose) println("You want verbose output")
      }
    }
  3. class FileOpt extends ArgOpt[File] with SingleOpt[File]

    Permalink

    FileOpt has the same usage as com.frugalmechanic.optparse.StrOpt except it returns a java.io.File instead of a String

  4. class IntOpt extends ArgOpt[Int] with SingleOpt[Int]

    Permalink

    IntOpt has the same usage as com.frugalmechanic.optparse.StrOpt except it returns a Int instead of a String

  5. trait MultiOpt[E] extends AnyRef

    Permalink
  6. class MultiStrOpt extends ArgOpt[Seq[String]] with MultiOpt[String]

    Permalink

    MultiStrOpt has the same usage as com.frugalmechanic.optparse.StrOpt except it returns a Seq[String] instead of just a String

  7. abstract class Opt extends AnyRef

    Permalink

    The base type of all command line options

  8. trait OptParse extends OptParseImplicits with OptParseTypes

    Permalink

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

    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

  9. trait OptParseImplicits extends AnyRef

    Permalink

    Contains implicits to make working with OptParse easier

  10. trait OptParseTypes extends AnyRef

    Permalink

    Type and Object aliases for OptParse that are mixed into OptParse to allow importing of only OptParse (import com.frugalmechanic.optparse.OptParse) and still have access to all of the types when you mix OptParse into your application object.

  11. abstract class OptVal[T] extends Opt

    Permalink

    Base type for a command line option that contains a value

  12. trait SingleOpt[T] extends AnyRef

    Permalink
  13. class StrOpt extends ArgOpt[String] with SingleOpt[String]

    Permalink

    A command line option that takes a string argument

    A command line option that takes a string argument

    Examples

    object MyApp extends OptParse {
      // Basic string argument using a default long name of --name and short name of -n
      val name = StrOpt()
    
      // Example with all available options instead of relying on the defaults
      val message = BoolOpt(
         long="message",          // Long name to use (--message)
         short="m",               // Short name to use (-m)
         default="Hello",         // Default value
         desc="Message to print", // Help message description
         enables=Nil,             // Other flags to enable if this one is enabled (single option or a Seq of options)
         disables=Nil,            // Other flags to disable if this one is enabled (single option or a Seq of options)
         invalidWith=Nil,         // Other options this flag is invalid with (they cannot be set)
         validWith=Nil,           // Other options that are required with this flag
         exclusive=false,         // Other options can be set when this option is set
         validate="^[a-zA-Z ,]+$" // Use a regex for validation via an implicit that converts it to: (String) => Boolean
      )
    
      def main(args:Array[String]) {
        parse(args)
    
        // getOrElse is available via an implicit from OptVal to Option[String]
        println(message+" "+name.getOrElse("Anonymous"))
      }
    }

Value Members

  1. object BoolOpt

    Permalink

    Companion object for creating BoolOpt's

    Companion object for creating BoolOpt's

    See com.frugalmechanic.optparse.BoolOpt for documentation and examples

  2. implicit def BoolOptToBool(opt: BoolOpt): Boolean

    Permalink

    Allows "if (MyFlag) ..."

    Allows "if (MyFlag) ..."

    Definition Classes
    OptParseImplicits
  3. object FileOpt

    Permalink

    See com.frugalmechanic.optparse.FileOpt for documentation and examples

  4. object IntOpt

    Permalink

    See com.frugalmechanic.optparse.IntOpt for documentation and examples

  5. object MultiStrOpt

    Permalink

    See com.frugalmechanic.optparse.MultiStrOpt for documentation and examples

  6. implicit def OptToBool[T](opt: OptVal[T]): Boolean

    Permalink

    Allows: if (NameOpt) ...

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

    Definition Classes
    OptParseImplicits
  7. implicit def OptToSeq[T](opt: Opt): Seq[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
  8. implicit def OptValToOption[T](opt: 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
  9. object StrOpt

    Permalink

    Companion object for creating StrOpt's

    Companion object for creating StrOpt's

    See com.frugalmechanic.optparse.StrOpt for documentation and examples

  10. 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
  11. 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

Inherited from OptParseImplicits

Inherited from AnyRef

Inherited from Any

Ungrouped