Question.scala 3.67 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
/**
2 3 4 5 6 7 8 9 10 11 12 13 14
  * Biopet is built on top of GATK Queue for building bioinformatic
  * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
  * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
  * should also be able to execute Biopet tools and pipelines.
  *
  * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
  *
  * Contact us at: sasc@lumc.nl
  *
  * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
  * license; For commercial users or users who do not want to follow the AGPL
  * license, please contact us to obtain a separate license.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
15 16 17
package nl.lumc.sasc.biopet.utils

/**
18 19
  * Created by pjvanthof on 16/12/2016.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
20 21
object Question {

Peter van 't Hof's avatar
Peter van 't Hof committed
22 23 24
  def string(name: String,
             default: Option[String] = None,
             description: Option[String] = None,
25
             possibleValues: List[String] = Nil,
Peter van 't Hof's avatar
Peter van 't Hof committed
26
             validation: List[(String) => Boolean] = Nil): String = {
Peter van 't Hof's avatar
Peter van 't Hof committed
27
    description.foreach(println)
28
    if (possibleValues.nonEmpty) println(s"possible values: ${possibleValues.mkString(", ")}")
Peter van 't Hof's avatar
Peter van 't Hof committed
29 30
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name > ")
31
    (scala.io.StdIn.readLine.trim, default) match {
Peter van 't Hof's avatar
Peter van 't Hof committed
32 33 34
      case (a, Some(d)) if a.isEmpty => d
      case (a, None) if a.isEmpty =>
        println("ERROR: Value is required")
35
        string(name, default, description, possibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
36 37 38
      case (a, _) =>
        if (!validation.forall(_(a))) {
          println("ERROR: Validation of failed")
39 40
          string(name, default, description, possibleValues, validation)
        } else if (possibleValues.nonEmpty && !possibleValues.contains(a)) {
Peter van 't Hof's avatar
Peter van 't Hof committed
41
          println("ERROR: Value not allowed")
42
          string(name, default, description, possibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
43 44 45 46
        } else a
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
47
  def boolean(name: String,
Peter van 't Hof's avatar
Peter van 't Hof committed
48 49
              default: Option[Boolean] = None,
              description: Option[String] = None): Boolean = {
Peter van 't Hof's avatar
Peter van 't Hof committed
50 51 52
    description.foreach(println)
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name (y/n) > ")
53
    scala.io.StdIn.readLine.trim.toLowerCase match {
54 55 56 57 58 59 60
      case "" =>
        default match {
          case Some(d) => d
          case _ =>
            println("ERROR: Value is required")
            boolean(name, default, description)
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
61 62 63 64 65 66 67 68 69 70 71
      case "y" | "yes" | "true" => true
      case "n" | "no" | "false" => false
      case _ =>
        println("ERROR: Value is a boolean value, please select 'y' of 'n'")
        boolean(name, default, description)
    }
  }

  def list(name: String,
           default: Option[List[String]] = None,
           description: Option[String] = None,
72 73
           possibleValues: List[String] = Nil,
           validation: (String) => Boolean = _ => true): List[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
74
    description.foreach(println)
75
    if (possibleValues.nonEmpty) println(s"possible values: ${possibleValues.mkString(", ")}")
Peter van 't Hof's avatar
Peter van 't Hof committed
76 77
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name > ")
78
    (scala.io.StdIn.readLine.split(",").toList.map(_.trim), default) match {
79 80
      case (List(""), Some(d)) => d
      case (List(""), None) =>
Peter van 't Hof's avatar
Peter van 't Hof committed
81
        println("ERROR: Value is required")
82
        list(name, default, description, possibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
83 84 85
      case (a, _) =>
        if (!a.forall(validation)) {
          println("ERROR: Validation of failed")
86 87
          list(name, default, description, possibleValues, validation)
        } else if (possibleValues.nonEmpty && !a.forall(possibleValues.contains)) {
Peter van 't Hof's avatar
Peter van 't Hof committed
88
          println("ERROR: Value not allowed")
89
          list(name, default, description, possibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
90 91 92 93
        } else a
    }
  }
}