Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Scala option parser based on Perl's Getopt::Long
Scala
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
LICENSE
README.adoc
build.sbt

README.adoc

scala-getoptions

Scala option parser based on Perl’s Getopt::Long.

Usage

  1. Define your command line specification:

    def print_version() = () => println("version is 0.2")
    
    def main(args: Array[String]) {
      val (options, remaining) = OptionParser.getOptions(args,
        Map(
          "-f|--flag"       -> 'flag,
          "-s|--string=s"   -> 'string,
          "-i|--int=i"      -> 'int,
          "-f|--float=f"    -> 'double,
          "-p|--procedure=p" -> { () => println("higher order function" }
          "-h=p"            -> { () => print_synopsis() }
          "--help|--man=p"  -> { () => launch_manpage() },
          "--version=p"     -> print_version,
        ))
  2. Pass cmdline arguments:

    $ script hello -f --string=mystring -i 7 --float 3.14 --p --version world -- --nothing
  3. It will run the higher order functions that were called on the cmdline:

    higher order function
    version is 0.2
  4. Internally it will return:

    remaining = Array("hello", "world", "--nothing")
    
    options = Map('flag -> true,
                  'string -> "mystring",
                  'int -> 7,
                  'double -> 3.14)

Description

It will take an Array[String] and a Map[String, Any] and it will return a Map[Symbol,Any] of the given arguments and an Array[String] of the remaining arguments.

Input / Output

Arguments
  1. Array[String].

  2. Map[String, Any], where Any can be Symbol, a higher order function or a method to a higher order function.

Returns
  1. Map[Symbol,Any] where Any can be a String, Integer or Double (And eventually Map and Array, see [_roadmap]).

  2. Array[String].

Features

  • Supports passing -- to stop parsing arguments (everything after will be left in the remaining Array[String]).

  • Multiple definitions for the same option separated by |. e.g. -f|--flag|--longer-version-of-flag.

  • Defining what kind of argument you are passing. Currently supports =s to pass strings, =i to pass integers, =f to pass Double values and =p to pass higher order functions.

  • Supports command line options with =. e.g. You can use --string=mystring and --string mystring.

Dependencies

SLF4J to add logging

libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.7"

How to install it

TODO

Roadmap

  • Remove - and -- out of the option definition:

    "s|string=s"

    instead of:

    "-s|--string=s"
  • Support bundling:

    -l -a -c  => -lac
  • Support passing values after the short option character:

    -i 24  => -i24
  • Support array arguments:

    --library lib/stdlib --library lib/extlib
    "library=s@ -> 'lib"
    Note
    This feature only takes one argument at a time. It is wrong to assume that calling --library lib/stdlib lib/extlib would end up with 'lib equals Array("lib/stdlib", "lib/extlib"). For that, Perl defines the experimental repeat specifier {}. e.g. "library=s{2}"
  • Support the repeat specifier {}:

    "rgbcolor=i{3}"
    --rgbcolor 255 255 149
  • Support hash (Map) arguments:

    --define os=linux --define vendor=redhat
    "define=s%"
  • Support negatable options:

    --noverbose
    --no-verbose
    "verbose!"
  • Support incremental options:

    -v -v -v
    "v|verbose+"
  • Optional value with ::

    If you specify "option:s +->+ 'opt" for example, you can call that with --option string where 'opt equals "string" or with just --option where 'opt equals "" (empty string).

    Additionally for an integer optional value, if the value is not specified the integer is set to [Int] 0.

  • Lone dash support -:

    -
    "" -> 'lone_dash
  • All other Perl’s Getopt::Long goodies that seem reasonable to add!

Something went wrong with that request. Please try again.