Base type for command line options that take an argument
Base type for command line options that take an argument
For implementation examples see:
A boolean/flag command line option
A boolean/flag command line option
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") } }
The base type of all command line options
OptParse provides simple command line parsing for Scala that only requires a minimal amount of code.
OptParse provides simple command line parsing for Scala that only requires a minimal amount of code.
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
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) } }
./myApp --help
or
./myApp -h
./myApp --flag --name Tim --aliases Timothy --aliases Timmy --number 123
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") } }
Contains implicits to make working with OptParse easier
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.
Base type for a command line option that contains a value
A command line option that takes a string argument
A command line option that takes a string argument
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")) } }
Companion object for creating BoolOpt's
Companion object for creating BoolOpt's
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
Companion object for creating StrOpt's
Companion object for creating StrOpt's
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")