Skip to content
Pierre Andrews edited this page Dec 30, 2013 · 2 revisions

Sumac is a command line option parser and library. It tries to differentiate itself from other libraries by making it dead simple to define arguments, removing boilerplate and repetition. It is a very small, lightweight scala library.

Usage

Define a basic container object which extends FieldArgs. Every field of the object becomes a command line argument with the same name. Then use parse() to process the command line arguments.

import com.quantifind.sumac.FieldArgs

class Arguments extends FieldArgs {
  var name: Option[String] = None
  var count: Int = _
}

object MyApp {
  def main(args: Array[String]) {
    val myArgs = new Arguments()
    myArgs.parse(args)
    ...
  }
}

Now MyApp has two arguments, "name" and "count". You can run it like this:

java -cp <path>/<to>/<compiled>/<code> MyApp --name foobar --count 17

ArgMain

You don't even have to call parse() yourself. The arguments are automatically parsed for you if you extend ArgMain

import com.quantifind.sumac.{ArgMain, FieldArgs}

class Arguments extends FieldArgs {
  var name: String = _
  var count: Int = _
}

object MyApp extends ArgMain[Arguments]{
   def main(args: Arguments) {
     //the cmd line arguments get parsed, and then passed into this function
     println(args.name)
   }
}

you could then run these programs with

java -cp <path>/<to>/<compiled>/<code> MyApp --name foobar --count 17

Parsing

Sumac support by default a number of parsers for the following field types:

  • Int, Long, Boolean, Float, Double
  • Scala Regular Expressions
  • java.io.File
  • java.util.Date and java.util.Calendar, with the formats: yyyy-MM-dd,yyyy/MM/dd, MM-dd-yyyy and MM/dd/yyyy
  • scala.concurrent.duration.Duration, with the format: 10.seconds, 1.day, etc.

Sumac can also parse "containers" (except for basic types), such as:

  • Option[A]
  • Enum[A]
  • List[A], Set[A], Array[A], Seq[A], Vector[A], Traversable[A], with the format --arg val1,val2,val3
  • Map[K,V], with the format: --arg key1:val1,key2:val2,key3:val3

Sumac provides an easy Validation mechanism to restrict the accepted values.

Clone this wiki locally