Question.scala 3.6 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

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
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
25
26
  def string(name: String,
             default: Option[String] = None,
             description: Option[String] = None,
             posibleValues: List[String] = Nil,
             validation: List[(String) => Boolean] = Nil): String = {
Peter van 't Hof's avatar
Peter van 't Hof committed
27
28
29
30
31
32
33
34
    description.foreach(println)
    if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name > ")
    (Console.readLine.trim, default) match {
      case (a, Some(d)) if a.isEmpty => d
      case (a, None) if a.isEmpty =>
        println("ERROR: Value is required")
Peter van 't Hof's avatar
Peter van 't Hof committed
35
        string(name, default, description, posibleValues, 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")
Peter van 't Hof's avatar
Peter van 't Hof committed
39
          string(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
40
41
        } else if (posibleValues.nonEmpty && !posibleValues.contains(a)) {
          println("ERROR: Value not allowed")
Peter van 't Hof's avatar
Peter van 't Hof committed
42
          string(name, default, description, posibleValues, 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    description.foreach(println)
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name (y/n) > ")
    Console.readLine.trim.toLowerCase match {
      case "" => default match {
        case Some(d) => d
        case _ =>
          println("ERROR: Value is required")
          boolean(name, default, description)
      }
      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,
           posibleValues: List[String] = Nil,
           validation: (String) => Boolean = String => true): List[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
73
74
75
76
77
    description.foreach(println)
    if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
    default.foreach(x => println(s"Default value: $x"))
    print(s"$name > ")
    (Console.readLine.split(",").toList.map(_.trim), default) match {
78
79
      case (List(""), Some(d)) => d
      case (List(""), None) =>
Peter van 't Hof's avatar
Peter van 't Hof committed
80
        println("ERROR: Value is required")
Peter van 't Hof's avatar
Peter van 't Hof committed
81
        list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
84
      case (a, _) =>
        if (!a.forall(validation)) {
          println("ERROR: Validation of failed")
Peter van 't Hof's avatar
Peter van 't Hof committed
85
          list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
86
87
        } else if (posibleValues.nonEmpty && !a.forall(posibleValues.contains)) {
          println("ERROR: Value not allowed")
Peter van 't Hof's avatar
Peter van 't Hof committed
88
          list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
89
90
91
92
        } else a
    }
  }
}