Subscribed unsubscribe Subscribe Subscribe

Scalaでgetoptっぽいかんじのやつ

Scala

scalaにgetoptあっても不思議じゃないと思ってたんだけど
パターンマッチでできるしいらないなって最近気づいた。今さら?

例として簡単なechoコマンド

#!/bin/sh
exec scala "$0" "$@"
!#

args.toList match {
  case "-n" :: xs => print(xs.mkString(" "))
  case xs => println(xs.mkString(" "))
}

実行結果

> ./echo 

> ./echo homu homu
homu homu
> ./echo -n homu homu
homu homu> 


もうちょっと長い例。
sbazでオプション解析してるとこ(sbaz/src/sbaz/clui/Settings.scala)

  /** Parse global options from the beginning of a command-line.
   * Returns the portion of the command line that was not
   * consumed.
   */
  def parseOptions(args: List[String]): List[String] =
    args match {
      case "-d" :: dirname :: rest =>
        this.dirname = new File(dirname)
        parseOptions(rest)

      case "-d" :: Nil =>
        println("Option -d requires an argument")
        sys.exit(1)

      case ("-n" | "--dryrun") :: rest =>
        dryrun = true
        parseOptions(rest)

      case ("-v" | "--verbose") :: rest =>
        verbose = true
        parseOptions(rest)

      case "-version" :: rest =>
        Console.println(versionMsg)
        sys.exit(0)

      case "--univ" :: name :: rest =>
        simpleSelector = SUS.WithName(name)
        parseOptions(rest)

      case "--univ" :: Nil =>
        println("Option --univ requires an argument")
	sys.exit(1)

      case "--univ-url" :: url :: rest =>
	simpleSelector = SUS.WithURL(url)
        parseOptions(rest)

      case "--univ-url" :: Nil =>
        println("Option --univ-url requires an argument")
	sys.exit(1)

      case _ =>
        args
    }