aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/options/Registration.scala
blob: 1ebccea4cdd00d0d0c64cda30abdb59925e030f5 (plain)
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
// SPDX-License-Identifier: Apache-2.0

package firrtl.options

import firrtl.{AnnotationSeq, Transform}

import scopt.{OptionDef, OptionParser, Read}

/** Contains information about a [[Shell]] command line option
  * @tparam the type of the command line argument
  * @param longOption a long, double-dash option
  * @param toAnnotationSeq a function to convert the type into an [[firrtl.AnnotationSeq AnnotationSeq]]
  * @param helpText help text
  * @param shortOption an optional single-dash option
  * @param helpValueName a string to show as a placeholder argument in help text
  */
final class ShellOption[A: Read](
  val longOption:      String,
  val toAnnotationSeq: A => AnnotationSeq,
  val helpText:        String,
  val shortOption:     Option[String] = None,
  val helpValueName:   Option[String] = None) {

  /** Add this specific shell (command line) option to an option parser
    * @param p an option parser
    */
  final def addOption(p: OptionParser[AnnotationSeq]): Unit = {
    val f = Seq(
      (p: OptionDef[A, AnnotationSeq]) => p.action((x, c) => toAnnotationSeq(x).reverse ++ c),
      (p: OptionDef[A, AnnotationSeq]) => p.text(helpText),
      (p: OptionDef[A, AnnotationSeq]) => p.unbounded()
    ) ++
      shortOption.map(a => (p: OptionDef[A, AnnotationSeq]) => p.abbr(a)) ++
      helpValueName.map(a => (p: OptionDef[A, AnnotationSeq]) => p.valueName(a))

    f.foldLeft(p.opt[A](longOption))((a, b) => b(a))
  }
}

/** Indicates that this class/object includes options (but does not add these as a registered class)
  */
trait HasShellOptions {

  /** A sequence of options provided
    */
  def options: Seq[ShellOption[_]]

  /** Add all shell (command line) options to an option parser
    * @param p an option parser
    */
  final def addOptions(p: OptionParser[AnnotationSeq]): Unit = options.foreach(_.addOption(p))

}

/** A [[Transform]] that includes an option that should be exposed at the top level.
  *
  * @note To complete registration, include an entry in
  * src/main/resources/META-INF/services/firrtl.options.RegisteredTransform
  */
trait RegisteredTransform extends HasShellOptions { this: Transform => }

/** A class that includes options that should be exposed as a group at the top level.
  *
  * @note To complete registration, include an entry in
  * src/main/resources/META-INF/services/firrtl.options.RegisteredLibrary
  */
trait RegisteredLibrary extends HasShellOptions {

  /** The name of this library.
    *
    * This will be used when generating help text.
    */
  def name: String

}