Download the JAR or add a Maven dependency. Also check out the Scaladocs.
import com.frugalmechanic.optparse._
object SimpleApp extends OptParse {
val name = StrOpt()
def main(args:Array[String]) {
parse(args)
println("Hello "+name.getOrElse("world"))
}
}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)
}
}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")
}
}