alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Opt.scala)

This example Scala source code file (Opt.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

boolean, error, fromstring, implicit, info, none, option, string, t, unit

The Opt.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools
package cmd

import nsc.Properties.envOrElse
import Spec.Info

/** Machinery for what amounts to a command line specification DSL.
 *  It is designed so the same specification trait can be used for
 *  two different purposes: generating a singleton specification object
 *  (trait Reference) and providing well typed vals for every configurable
 *  option in response to any given set of arguments (trait Instance).
 */
object Opt {
  trait Error {
    self: Implicit =>

    protected def fail(msg: String) = runAndExit(println(programInfo.runner + ": " + msg))
    protected def failOption(arg: String, why: String) = fail("%s: '%s' is %s".format(opt, arg, why))
  }

  trait Implicit {
    def name: String
    def programInfo: Info
    protected def opt = fromOpt(name)

    def --? : Boolean                       // --opt is set
    def --> (body: => Unit): Boolean        // if --opt is set, execute body
    def --| : Option[String]                // --opt <arg: String> is optional, result is Option[String]
    def --^[T: FromString] : Option[T]      // --opt <arg: T> is optional, result is Option[T]

    def optMap[T](f: String => T) = --| map f

    /** Names.
     */
    def defaultTo[T: FromString](default: T): T
    def defaultToEnv(envVar: String): String
    def choiceOf[T: FromString](choices: T*): Option[T]
    def expandTo(args: String*): Unit

    /** Help.
     */
    def /(descr: String): String            // --opt has help description 'descr'
  }

  class Reference(val programInfo: Info, val options: Reference.Accumulators, val name: String) extends Implicit {
    import options._

    def --?                             = { addUnary(opt) ; false }
    def --> (body: => Unit)             = { addUnary(opt) ; false }
    def --|                             = { addBinary(opt) ; None }
    def --^[T: FromString]              = { addBinary(opt) ; None }

    def defaultTo[T: FromString](default: T)  = { addBinary(opt) ; addHelpDefault(() => default.toString) ; default }
    def defaultToEnv(envVar: String)          = { addBinary(opt) ; addHelpEnvDefault(envVar) ; "" }
    def choiceOf[T: FromString](choices: T*)  = { addBinary(opt) ; None }
    def expandTo(args: String*)               = { addExpand(name, args.toList) ; addHelpAlias(() => args mkString " ") }

    def /(descr: String)                = returning(name)(_ => addHelp(() => helpFormatStr.format(opt, descr)))
  }

  class Instance(val programInfo: Info, val parsed: CommandLine, val name: String) extends Implicit with Error {
    def --?                             = parsed isSet opt
    def --> (body: => Unit)             = { val isSet = parsed isSet opt ; if (isSet) body ; isSet }
    def --|                             = parsed get opt
    def --^[T: FromString]              = {
      val fs = implicitly[FromString[T]]
      --| map { arg =>
        if (fs isDefinedAt arg) fs(arg)
        else failOption(arg, "not a " + fs.targetString)
      }
    }

    def defaultTo[T: FromString](default: T)  = --^[T] getOrElse default
    def defaultToEnv(envVar: String)          = --| getOrElse envOrElse(envVar, "")
    def expandTo(args: String*)               = ()

    def choiceOf[T: FromString](choices: T*) = {
      --^[T] map { arg =>
        if (choices contains arg) arg
        else failOption(arg.toString, "not a valid choice from " + choices)
      }
    }

    def /(descr: String)                = name
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala Opt.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.