Commit 958b9456 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Added boolean

parent 7fa75aed
......@@ -180,7 +180,7 @@ object Reference {
def askReference: Map[String, Any] = {
val warn = "It's possible to select something else but be aware of installing all fasta / index files required by a pipeline."
val globalSpecies = Config.global.defaults.getOrElse("references", Map()).asInstanceOf[Map[String, Any]]
val species = Question.askValue("species",
val species = Question.string("species",
description = Some(if (globalSpecies.nonEmpty)
s"""Species found in general config:
|- ${globalSpecies.keys.mkString("\n- ")}
......@@ -188,7 +188,7 @@ object Reference {
|""".stripMargin else s"No references found in global config. $warn"))
val globalReferences = globalSpecies.getOrElse(species, Map()).asInstanceOf[Map[String, Any]]
val referenceName = Question.askValue("reference_name",
val referenceName = Question.string("reference_name",
description = Some(if (globalReferences.nonEmpty)
s"""Reference for $species found in general config:
|- ${globalReferences.keys.mkString("\n- ")}
......@@ -197,7 +197,7 @@ object Reference {
val reference = globalReferences.getOrElse(referenceName, Map()).asInstanceOf[Map[String, Any]]
val referenceFasta: Option[String] = if (reference.contains("reference_fasta")) None else {
Some(Question.askValue("Reference Fasta", validation = List(TemplateTool.isAbsolutePath, TemplateTool.mustExist),
Some(Question.string("Reference Fasta", validation = List(TemplateTool.isAbsolutePath, TemplateTool.mustExist),
description = Some(s"No fasta file found for $species -> $referenceName")))
}
......
......@@ -27,7 +27,7 @@ trait TemplateTool extends ToolCommand {
val argsParser = new OptParser
val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse (throw new IllegalArgumentException)
val standard: Map[String, Any] = Map("output_dir" -> Question.askValue("Output directory", validation = List(isAbsolutePath, parentIsWritable)))
val standard: Map[String, Any] = Map("output_dir" -> Question.string("Output directory", validation = List(isAbsolutePath, parentIsWritable)))
val config = pipelineMap(standard, cmdArgs.expert)
println(ConfigUtils.mapToYaml(config))
......
......@@ -5,11 +5,11 @@ package nl.lumc.sasc.biopet.utils
*/
object Question {
def askValue(name: String,
default: Option[String] = None,
description: Option[String] = None,
posibleValues: List[String] = Nil,
validation: List[(String) => Boolean] = Nil): String = {
def string(name: String,
default: Option[String] = None,
description: Option[String] = None,
posibleValues: List[String] = Nil,
validation: List[(String) => Boolean] = Nil): String = {
description.foreach(println)
if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
default.foreach(x => println(s"Default value: $x"))
......@@ -18,23 +18,44 @@ object Question {
case (a, Some(d)) if a.isEmpty => d
case (a, None) if a.isEmpty =>
println("ERROR: Value is required")
askValue(name, default, description, posibleValues, validation)
string(name, default, description, posibleValues, validation)
case (a, _) =>
if (!validation.forall(_(a))) {
println("ERROR: Validation of failed")
askValue(name, default, description, posibleValues, validation)
string(name, default, description, posibleValues, validation)
} else if (posibleValues.nonEmpty && !posibleValues.contains(a)) {
println("ERROR: Value not allowed")
askValue(name, default, description, posibleValues, validation)
string(name, default, description, posibleValues, validation)
} else a
}
}
def askList(name: String,
default: Option[List[String]] = None,
description: Option[String] = None,
posibleValues: List[String] = Nil,
validation: (String) => Boolean = String => true): List[String] = {
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] = {
description.foreach(println)
if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
default.foreach(x => println(s"Default value: $x"))
......@@ -43,14 +64,14 @@ object Question {
case (Nil, Some(d)) => d
case (Nil, None) =>
println("ERROR: Value is required")
askList(name, default, description, posibleValues, validation)
list(name, default, description, posibleValues, validation)
case (a, _) =>
if (!a.forall(validation)) {
println("ERROR: Validation of failed")
askList(name, default, description, posibleValues, validation)
list(name, default, description, posibleValues, validation)
} else if (posibleValues.nonEmpty && !a.forall(posibleValues.contains)) {
println("ERROR: Value not allowed")
askList(name, default, description, posibleValues, validation)
list(name, default, description, posibleValues, validation)
} else a
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment