src/Pure/System/options.scala
author wenzelm
Mon, 23 Jul 2012 22:35:10 +0200
changeset 48456 d8ff14f44a40
parent 48421 c4d337782de4
child 48457 fd9e28d5a143
permissions -rw-r--r--
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/options.scala
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     3
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     4
Stand-alone options with external string representation.
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     5
*/
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     6
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     7
package isabelle
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     8
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     9
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48373
diff changeset
    10
import java.io.{File => JFile}
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    11
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    12
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    13
object Options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    14
{
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    15
  type Spec = (String, Option[String])
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    16
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    17
  val empty: Options = new Options()
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    18
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    19
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    20
  /* representation */
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    21
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    22
  sealed abstract class Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    23
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    24
    def print: String = toString.toLowerCase
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    25
  }
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    26
  private case object Bool extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    27
  private case object Int extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    28
  private case object Real extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    29
  private case object String extends Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    30
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    31
  case class Opt(typ: Type, value: String, description: String)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    32
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    33
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    34
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    35
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    36
  private object Parser extends Parse.Parser
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    37
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    38
    val DECLARE = "declare"
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    39
    val DEFINE = "define"
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    40
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    41
    val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    42
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    43
    val entry: Parser[Options => Options] =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    44
    {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    45
      val option_name = atom("option name", _.is_xname)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    46
      val option_type = atom("option type", _.is_ident)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    47
      val option_value = atom("option value", tok => tok.is_name || tok.is_float)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    48
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    49
      keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    50
      keyword("=") ~ option_value ~ opt(text)) ^^
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    51
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    52
            (options: Options) => options.declare(a, b, c, d.getOrElse("")) } |
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    53
      keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    54
        { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    55
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    56
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48373
diff changeset
    57
    def parse_entries(file: JFile): List[Options => Options] =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    58
    {
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents: 48409
diff changeset
    59
      parse_all(rep(entry), Token.reader(syntax.scan(File.read(file)), file.toString)) match {
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    60
        case Success(result, _) => result
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    61
        case bad => error(bad.toString)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    62
      }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    63
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    64
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    65
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    66
  private val OPTIONS = Path.explode("etc/options")
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    67
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    68
  def init(): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    69
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    70
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    71
    for {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    72
      dir <- Isabelle_System.components()
48373
527e2bad7cca further imitation of "usedir" shell script;
wenzelm
parents: 48370
diff changeset
    73
      file = (dir + OPTIONS).file
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    74
      if file.isFile
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    75
      entry <- Parser.parse_entries(file)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    76
    } {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    77
      try { options = entry(options) }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    78
      catch { case ERROR(msg) => error(msg + Position.str_of(Position.file(file))) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    79
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    80
    options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    81
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    82
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    83
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    84
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    85
final class Options private(options: Map[String, Options.Opt] = Map.empty)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    86
{
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    87
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    88
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    89
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    90
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    91
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    92
  private def check_name(name: String): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    93
    options.get(name) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    94
      case Some(opt) => opt
48368
dc538eef2cf2 define build_options from command line;
wenzelm
parents: 48365
diff changeset
    95
      case None => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    96
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    97
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    98
  private def check_type(name: String, typ: Options.Type): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    99
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   100
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   101
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   102
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   103
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   104
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   105
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   106
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   107
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   108
  private def put[A](name: String, typ: Options.Type, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   109
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   110
    val opt = check_type(name, typ)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   111
    new Options(options + (name -> opt.copy(value = value)))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   112
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   113
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   114
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   115
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   116
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   117
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   118
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   119
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   120
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   121
          " : " + typ.print + " =\n" + quote(opt.value))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   122
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   123
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   124
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   125
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   126
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   127
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   128
  val bool = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   129
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   130
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   131
    def update(name: String, x: Boolean): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   132
      put(name, Options.Bool, Properties.Value.Boolean(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   133
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   134
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   135
  val int = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   136
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   137
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   138
    def update(name: String, x: Int): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   139
      put(name, Options.Int, Properties.Value.Int(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   140
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   141
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   142
  val real = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   143
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   144
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   145
    def update(name: String, x: Double): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   146
      put(name, Options.Real, Properties.Value.Double(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   147
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   148
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   149
  val string = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   150
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   151
    def apply(name: String): String = get(name, Options.String, s => Some(s))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   152
    def update(name: String, x: String): Options = put(name, Options.String, x)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   153
  }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   154
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   155
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   156
  /* external declare and define */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   157
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   158
  private def check_value(name: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   159
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   160
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   161
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   162
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   163
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   164
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   165
      case Options.String => string(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   166
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   167
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   168
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   169
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   170
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   171
    options.get(name) match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   172
      case Some(_) => error("Duplicate declaration of option " + quote(name))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   173
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   174
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   175
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   176
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   177
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   178
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   179
            case "string" => Options.String
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   180
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   181
          }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   182
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   183
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   184
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   185
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   186
  def define(name: String, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   187
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   188
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   189
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   190
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   191
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   192
  def define(name: String, opt_value: Option[String]): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   193
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   194
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   195
    opt_value match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   196
      case Some(value) => define(name, value)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   197
      case None if opt.typ == Options.Bool => define(name, "true")
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   198
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   199
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   200
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   201
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   202
  def ++ (specs: List[Options.Spec]): Options =
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   203
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   204
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   205
  def define_simple(str: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   206
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   207
    str.indexOf('=') match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   208
      case -1 => define(str, None)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   209
      case i => define(str.substring(0, i), str.substring(i + 1))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   210
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   211
  }
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   212
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   213
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   214
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   215
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   216
  def encode: XML.Body =
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   217
  {
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   218
    import XML.Encode.{string => str, _}
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   219
    list(triple(str, str, str))(
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   220
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   221
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   222
}