Search code examples
scalacommand-linesbtuser-input

prompt for user input when running scala program with sbt


I have a very simple scala program:

object TakeInputs {
  def main(args: Array[String]) {
    val name = readLine("What is your name?")
    println(name)
  }
}

When I try to run this with sbt "project myproject" "run-main TakeInput" it doesn't wait for user input and the program just finishes with What is your name?null as the output.

Is there a way to make sbt wait for user input (like what happens if "readLine" is run in sbt console)? I can provide the inputs as command line parameters but I have a lot of them and I would like to make the program more user-friendly by displaying messages indicating what the user should enter next. Thanks.


Solution

  • Add the following to your build.sbt

    connectInput in run := true
    

    From the sbt documentation in Configuring Input