scala0.cmdline

620 views

Published on

Boston Area Scala Enthusiasts second meeting held on Dec 2, 2009.
Talk delivered by Jeremy Cloud.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
620
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

scala0.cmdline

  1. 1. jeremy.cloud { • ~2 years professional development with Scala • freelance software developer / web designer -- hire me! • jeremy.cloud@me.com } Friday, December 4, 2009
  2. 2. Friday, December 4, 2009
  3. 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. 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. 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. 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. 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. 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
  9. 9. http://scala0.net thank you Friday, December 4, 2009

×