Question.scala 2.93 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
package nl.lumc.sasc.biopet.utils

/**
  * Created by pjvanthof on 16/12/2016.
  */
object Question {

Peter van 't Hof's avatar
Peter van 't Hof committed
8
9
10
11
12
  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
13
14
15
16
17
18
19
20
    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
21
        string(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
22
23
24
      case (a, _) =>
        if (!validation.forall(_(a))) {
          println("ERROR: Validation of failed")
Peter van 't Hof's avatar
Peter van 't Hof committed
25
          string(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
26
27
        } else if (posibleValues.nonEmpty && !posibleValues.contains(a)) {
          println("ERROR: Value not allowed")
Peter van 't Hof's avatar
Peter van 't Hof committed
28
          string(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30
31
32
        } else a
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  def boolean(name: String,
             default: Option[Boolean] = None,
             description: Option[String] = None): Boolean = {
    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
59
60
61
62
63
    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 {
64
65
      case (List(""), Some(d)) => d
      case (List(""), None) =>
Peter van 't Hof's avatar
Peter van 't Hof committed
66
        println("ERROR: Value is required")
Peter van 't Hof's avatar
Peter van 't Hof committed
67
        list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
70
      case (a, _) =>
        if (!a.forall(validation)) {
          println("ERROR: Validation of failed")
Peter van 't Hof's avatar
Peter van 't Hof committed
71
          list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
72
73
        } else if (posibleValues.nonEmpty && !a.forall(posibleValues.contains)) {
          println("ERROR: Value not allowed")
Peter van 't Hof's avatar
Peter van 't Hof committed
74
          list(name, default, description, posibleValues, validation)
Peter van 't Hof's avatar
Peter van 't Hof committed
75
76
77
78
        } else a
    }
  }
}