simple scala command line options parsing
Scala
Switch branches/tags
Nothing to show
Pull request Compare This branch is 5 commits behind jstrachan:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
notes
project
src
.gitignore
Karaf.md
README.md
build.sbt

README.md

scopt

scopt is a little command line options parsing library.

Its based on the code from Tim Perrett which his based on Aaron Harnly's code mentioned in this thread and this thread which is available as a gist or here.

If you prefer the Annotation approach

Since hacking on scopt I've now moved to the Annotation Approach via Karaf you might like to check that out if you find limitations in scopt.

Usage

Create an OptionParser and customise it with the options you need, passing in functions to process each option or argument.

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("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("<singlefile>", "<singlefile> is an argument", {v: String => config.whatnot = v})
  // arglist("<file>...", "arglist allows variable number of arguments",
  //   {v: String => config.files = (v :: config.files).reverse })
}
if (parser.parse(args)) {
   // do stuff
}
else {
  // arguments are bad, usage message will have been displayed
}

The above generates the following usage text:

Usage: scopt [options] <filename>

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

scala-tools.org Nexus

val scopt = "com.github.scopt" %% "scopt" % "1.1.0"
val scalaToolsNexusSnapshots = "Scala Tools Nexus Snapshots" at "http://nexus.scala-tools.org/content/repositories/snapshots/"
val scalaToolsNexusReleases  = "Scala Tools Nexus Releases" at "http://nexus.scala-tools.org/content/repositories/releases/"

Building

You should be able to use sbt to build scopt.

License

Do whatever you like with it :), or use MIT License.

Changes

See notes.