Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 99 lines (76 sloc) 3.252 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
* @author Paul Phillips
*/

package scala.tools
package cmd

import scala.collection.mutable.ListBuffer
import nsc.Properties.envOrNone

/** Mixes in the specification trait and uses the vals therein to
* side-effect private accumulators. From this emerges formatted help,
* lists of unary and binary arguments, an apply which can creates
* instances of the specification, and etc.
*
* @see Instance
*/
trait Reference extends Spec {
  lazy val options = new Reference.Accumulators()
  import options._

  def helpMsg = options.helpMsg
  def propertyArgs: List[String] = Nil

  def isUnaryOption(s: String) = unary contains toOpt(s)
  def isBinaryOption(s: String) = binary contains toOpt(s)
  def isExpandOption(s: String) = expansionMap contains toOpt(s)

  def expandArg(arg: String) = expansionMap.getOrElse(fromOpt(arg), List(arg))

  protected def help(str: => String) = addHelp(() => str)

  type ThisCommandLine <: CommandLine

  class SpecCommandLine(args: List[String]) extends CommandLine(Reference.this, args) { }
  protected def creator(args: List[String]): ThisCommandLine
  final def apply(args: String*): ThisCommandLine = creator(propertyArgs ++ args flatMap expandArg)

  type OptionMagic = Opt.Reference
  protected implicit def optionMagicAdditions(name: String) = new Opt.Reference(programInfo, options, name)
}

object Reference {
  val MaxLine = 80

  class Accumulators() {
    private val _help = new ListBuffer[() => String]
    private var _unary = List[String]()
    private var _binary = List[String]()
    private var _expand = Map[String, List[String]]()

    def helpFormatStr = " %-" + longestArg + "s %s"
    def defaultFormatStr = (" " * (longestArg + 7)) + "%s"

    def addUnary(s: String) = _unary +:= s
    def addBinary(s: String) = _binary +:= s

    def addExpand(opt: String, expanded: List[String]) =
      _expand += (opt -> expanded)

    def mapHelp(g: String => String) = {
      val idx = _help.length - 1
      val f = _help(idx)

      _help(idx) = () => g(f())
    }

    def addHelp(f: () => String) = _help += f
    def addHelpAlias(f: () => String) = mapHelp { s =>
      val str = "alias for '%s'" format f()
      def noHelp = (helpFormatStr.format("", "")).length == s.length
      val str2 = if (noHelp) str else " (" + str + ")"

      s + str2
    }
    def addHelpDefault(f: () => String) = mapHelp { s =>
      val str = "(default: %s)" format f()

      if (s.length + str.length < MaxLine) s + " " + str
      else defaultFormatStr.format(s, str)
    }
    def addHelpEnvDefault(name: String) = mapHelp { s =>
      val line1 = "%s (default: %s)".format(s, name)
      val envNow = envOrNone(name) map ("'" + _ + "'") getOrElse "unset"
      val line2 = defaultFormatStr.format("Currently " + envNow)

      line1 + "\n" + line2
    }

    lazy val unary = (_unary ++ _expand.keys).distinct
    lazy val binary = _binary.distinct
    lazy val all = unary ++ binary
    lazy val expansionMap = _expand
    lazy val helpMsg = _help map (f => f() + "\n") mkString
    lazy val longestArg = all map (_.length) max
  }
}
Something went wrong with that request. Please try again.