More than Just Lines on a Map: Best Practices for U.S Bike Routes
scala0.cmdline
1. jeremy.cloud {
• ~2 years professional development
with Scala
• freelance software developer / web
designer -- hire me!
• jeremy.cloud@me.com
}
Friday, December 4, 2009
3. scala0.cmdline:
• distinct command-line modes
• support complex dependencies between
options
• validation using typed failures so you can
customize error messages
• generates nicely formatted usage message
• short and/or long names
• dependency free
Friday, December 4, 2009
4. distinct command-line modes
foo -v
foo -?
foo -c [-f num] filenames...
foo [-d dir] [-l limit] filename
command-line parser will tell you which
variation is being used
Friday, December 4, 2009
5. dependencies between options
if a then b:
foo [-a blah -b bleh]
b only if a:
foo [-a blah [-b bleh]]
one of a, b, c:
foo (-a blah | -b bleh | -c)
arbitrarily nested:
foo [-a (-b | -c -d)]
Friday, December 4, 2009
6. uses internal DSL
object Options extends CommandLineParser {
val version = Flag("v", "version", "Print version and quit")
val help = Flag("?", "help", "Print help and quit")
val a = Flag("a", None, "A")
val b = Flag("b", None, "B")
val c = Flag("c", None, "C")
val d = Flag("d", None, "D")
val filename = NakedArgument("filename", "The name of the file")
val versionMode = %(version)
val helpMode = %(help)
val defaultMode = %((a ~ (b | c ~ d)).?, filename)
val spec = %%(versionMode, helpMode, defaultMode)
}
Friday, December 4, 2009
7. mode and error matching
def main(args: Array[String]) {
Options(args) match {
case Left(error) =>
Console.err.println(error)
Options.printHelp("foo")
case Right((Options.versionMode, _)) =>
Console.out.println("Version 1.0")
case Right((Options.helpMode, _)) =>
Options.printHelp("foo")
case Right((Options.defaultMode, cmdLine)) =>
val isA = cmdLine(Options.a)
val filename = cmdLine(Options.filename).head
// do something...
}
}
Friday, December 4, 2009
8. generates nice help messages
USAGE:
foo -v
foo -?
foo [-a (-b | -c -d)] filename
OPTIONS:
-?, --help
Print help and quit
-a, --accelerator
enable accelerator
-b, --blaster
use blaster
-c, --cool
make it cool
-d, --dump
dumb it down
-v, --version
Print version and quit
Friday, December 4, 2009