Allows "if(MyFlag) .
Allows "if(MyFlag) ..."
Allows: if(NameOpt) .
Allows: if(NameOpt) ... instead of if(NameOpt.value.isDefined) ...
Allows BoolOpt(enables=MyOpt) instead of BoolOpt(enables=Seq(MyOpt))
Allows BoolOpt(enables=MyOpt) instead of BoolOpt(enables=Seq(MyOpt))
Allows any Option methods to be used on an OptVal
Allows any Option methods to be used on an OptVal
Allows regex usage for the validate option: validate="^[a-zA-Z]+$"
Allows regex usage for the validate option: validate="^[a-zA-Z]+$"
Allows longName="name" without needing long=Some("name")
Allows longName="name" without needing long=Some("name")
Declare a "default option"
Exit the application with a message
Exit the application with a message
Print the help message to STDERR
A default help option (--help or -h) that displays the help message
Override to true to enable simple println's showing what OptParse is doing
By default OptParse will do a System.
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
Parse the given command line options
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:
or
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
or
Pass in some options
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