summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/cmd/program/Simple.scala
blob: f2095d64b86c08a982e7e9352d15ced6454d6cf4 (plain) (blame)
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
/* 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
  }
}