All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.sciss.mellite.gui.impl.ObjViewCmdLineParser.scala Maven / Gradle / Ivy

The newest version!
/*
 *  ObjViewCmdLineParser.scala
 *  (Mellite)
 *
 *  Copyright (c) 2012-2019 Hanns Holger Rutz. All rights reserved.
 *
 *  This software is published under the GNU Affero General Public License v3+
 *
 *
 *  For further information, please contact Hanns Holger Rutz at
 *  [email protected]
 */

package de.sciss.mellite.gui.impl

import de.sciss.lucre.stm.Sys
import de.sciss.mellite.gui.{MessageException, ObjView}
import de.sciss.processor.Processor.Aborted
import scopt.{OptionDef, OptionParser}

import scala.util.{Failure, Success, Try}

object ObjViewCmdLineParser {
  def apply[S <: Sys[S]](f: ObjView.Factory): ObjViewCmdLineParser[f.Config[S]] =
    new ObjViewCmdLineParser[f.Config[S]](f)
}
class ObjViewCmdLineParser[C](private val f: ObjView.Factory)
  extends OptionParser[C](f.prefix) {

  private[this] var _error    = ""
  private[this] var _aborted  = false

  override def terminate(exitState: Either[String, Unit]): Unit =
    if (exitState.isRight) _aborted = true

  override def reportError(msg: String): Unit = _error = msg

  def name(action: (String, C) => C): OptionDef[String, C] =
    opt[String]('n', "name")
      .text(s"Object's name (default: ${f.prefix})")
      .action(action)

  def parseConfig(args: List[String], default: C): Try[C] = {
    val opt = parse(args, default)
    if (_aborted) Failure(Aborted()) else opt match {
      case Some(value)  => Success(value)
      case None         => Failure(MessageException(_error))
    }
  }

  // how annoying can it be...

  private[this] var showedUsage = false

  override def showUsage(): Unit = if (!showedUsage) {
    super.showUsage()
    showedUsage = true
  }

  override def showUsageAsError(): Unit = if (!showedUsage) {
    super.showUsageAsError()
    showedUsage = true
  }

  override def showUsageOnError: Boolean = true // even if we offer '--help'

  // constructor
  help("help").text("Prints this usage text")

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy