src/Pure/System/command_line.scala
author wenzelm
Wed, 23 Apr 2014 12:55:57 +0200
changeset 56669 f42717b5dc84
parent 56631 89269bb8e7ca
child 56671 06853449cf0a
permissions -rw-r--r--
clarified message and return code, in accordance to ML version;

/*  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 =>
          if (debug) exn.printStackTrace
          System.err.println(cat_lines(split_lines(Exn.message(exn)).map("*** " + _)))
          Exn.return_code(exn, 2)
      }
    sys.exit(rc)
  }

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