src/Pure/System/command_line.scala
author wenzelm
Sat, 28 Mar 2020 19:33:14 +0100
changeset 71612 e0a5d6068141
parent 68806 4597812d5182
child 71632 c1bc38327bc2
permissions -rw-r--r--
tuned;

/*  Title:      Pure/System/command_line.scala
    Author:     Makarius

Support for Isabelle/Scala command line tools.
*/

package isabelle


object Command_Line
{
  object Chunks
  {
    private def chunks(list: List[String]): List[List[String]] =
      list.indexWhere(_ == "\n") match {
        case -1 => List(list)
        case i =>
          val (chunk, rest) = list.splitAt(i)
          chunk :: chunks(rest.tail)
      }
    def unapplySeq(list: List[String]): Option[List[List[String]]] = Some(chunks(list))
  }

  var debug = false

  def tool(body: => Int): Nothing =
  {
    val rc =
      try { body }
      catch {
        case exn: Throwable =>
          Output.error_message(Exn.message(exn) + (if (debug) "\n" + Exn.trace(exn) else ""))
          Exn.return_code(exn, 2)
      }
    sys.exit(rc)
  }

  def tool0(body: => Unit): Nothing = tool { body; 0 }

  def ML_tool0(body: List[String]): String =
    "Command_Line.tool0 (fn () => (" + body.mkString("; ") + "));"
}