parsing parameter using scopt

Of three command line parameter parsing libraries that I found in Scala, I found jstrachan/scopt to be the best one out there: lightweight and easy to use. The new feature I will be working on, however required -x:key=value option, which was not supported by scopt. The great thing about opensource is that I can just add the new feature if I want to, and github makes it so easy to fork and add a few things in.

So I did. First I updated the sbt project to build against 2.8.0.Beta1. Then, I updated the usage text style to look more like those of gcc --help and java -help. For example, the usage text now includes the name of the program, the default value name is <value> instead of VALUE, and option list is indented. Optionally you can also supply the value name as well instead of <value> to say something like <outdir>. Finally, implemented the -x:key=value option.

To use scopt, you write code that look like:

val parser = new OptionParser("scopt") {
  intOpt("f", "foo", "foo is an integer property", {v: Int => config.foo = v})
  opt("o", "output", "<file>", "output is a string property", {v: String => config.bar = v})
  booleanOpt("x", "xyz", "xyz is a boolean property", {v: Boolean => config.xyz = v})
  keyValueOpt("l", "lib", "<libname>", "<filename>", "load library <libname>",
    {(key: String, value: String) => { config.libname = key; config.libfile = value } })
  arg("<filename>", "some argument", {v: String => config.whatnot = v})
}
if (parser.parse(args)) {
   // do stuff
}
else {
  // arguments are bad, usage message will have been displayed
}

and this automatically generates usage text that looks like

Usage: scopt [options] <filename>

  -f <value> | --foo <value>
        foo is an integer property
  -o <file> | --output <file>
        output is a string property
  -x <value> | --xyz <value>
        xyz is a boolean property
  -l:<libname>=<filename> | --lib:<libname>=<filename>
        load library <libname>
  <file>
        some argument

I am not sure my changes will be accepted by the upsteam, but I am putting it into scalaxb soon.