Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
simple scala command line options parsing
Scala
tag: 1.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
notes
project
src
.gitignore
Karaf.md
README.md

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.0.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

  • added -x:key=value option
  • updated usage text style
  • added maven and sbt builds
  • added ScalaTest test cases
  • added arguments which then are displayed in help
  • minor refactoring of names; opt and arg for options and args
  • updated sbt and maven to build against Scala 2.8.0
  • added option that does not have short option
Something went wrong with that request. Please try again.