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

Scala example source code file (Simple.scala)

This example Scala source code file (Simple.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

boolean, boolean, commandlinetransform, info, list, list, simplecommandline, simplecommandline, simpleinstance, simplereference, simplespec, string, string, ticket2338wontfixworkaround

The Scala Simple.scala source code

/* NEST (New Scala Test)
 * Copyright 2007-2011 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools
package cmd
package program

import Spec.Info

/** A boilerplate reducer for commands with simple requirements.  For examples,
 *  see Scmp and Tokens in this package.
 */
object Simple {
  type CommandLineTransform = SimpleCommandLine => SimpleCommandLine
  
  abstract class SimpleSpec(val programInfo: Info) extends Spec with Meta.StdOpts with Interpolation
  
  trait SimpleInstance extends SimpleSpec with Instance {
    val parsed: CommandLine
  }
  
  class SimpleReference(
    programInfo: Info,
    unary: List[(String, String)] = Nil, 
    binary: List[(String, String)] = Nil,
    postCreation: CommandLineTransform = null
  ) extends SimpleSpec(programInfo) with Reference {

    spec => 
    
    if (programInfo.usage != "") help(programInfo.usage)
    unary foreach   { case (option, help) => option / help --? }
    binary foreach  { case (option, help) => option / help --| }
    
    type ThisCommandLine = SimpleCommandLine
  
    def creator(args: List[String]) = new SimpleCommandLine(spec, args)
    def instance(args: Array[String]): SimpleInstance = instance(args.toList)
    def instance(args: List[String]): SimpleInstance =
      new {
        val parsed = spec(args: _*)
      } with SimpleSpec(programInfo) with SimpleInstance {
        lazy val referenceSpec = spec
      }
    
    lazy val referenceSpec = spec    
  }
  
  def apply(info: Info, unary: List[(String, String)], binary: List[(String, String)], postCreation: CommandLineTransform): SimpleReference = {
    new SimpleReference(info, unary, binary, postCreation) {
      override def creator(args: List[String]) = {
        val obj = super.creator(args)
        if (postCreation == null) obj
        else postCreation(obj)
      }
    }
  }
  
  def scalaProgramInfo(name: String, help: String) =
    Spec.Info(name, help, "scala.tools.cmd.program." + name.capitalize)

  /** You can't override a def with a var unless a setter exists.  We cleverly
   *  sidestep this by mixing in a trait with dummy setters which will be
   *  inaccessible due to the overriding var.
   */
  trait Ticket2338WontFixWorkaround {
    def enforceArity_=(x: Boolean): Unit = sys.error("unreachable")
    def onlyKnownOptions_=(x: Boolean): Unit = sys.error("unreachable")
  }

  /** Configurability simplicity achieved by turning defs into vars and letting
   *  the spec creator apply a transformation.  This way there's no need to create
   *  custom subclasses of CommandLine.
   */
  class SimpleCommandLine(spec: Reference, args: List[String]) extends CommandLine(spec, args) with Ticket2338WontFixWorkaround {
    override var enforceArity: Boolean = true
    override var onlyKnownOptions: Boolean = true
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.