A small program that can be packed as a jar and you run it in the console like this:
scala program.jar -v --debug --file "/home/user/..."
Is there a way to get the whole command with all the parameters as a String?
object Main {
def main(args: Array[String]): Unit = {
println("???")
// should print: "scala program.jar -v --debug --file "/home/user/..."
}
}
You can't reliably get the complete command line (see this discussion) but you can get close.
object Main {
def main(args: Array[String]): Unit = {
val allargs = "scala" +: System.getProperty("sun.java.command").split(" ").tail
println(allargs.mkString(" "))
}
}
Use mkstring method of array. As argument you can place separator between array entities.
Ex: http://ideone.com/
val argss = Array("a", "b", "c --d")
println(argss.mkString(" "))
Related
I am trying to execute sample code on databricks in scala. It is an object.
object Main {
def main(args: Array[String]) {
val res = for (a <- args) yield a.toUpperCase
println("Arguments: " + res.toString)
}
}
When I run on databricks; it says 'object defined main'. I am not sure how to execute it now or what is the code to execute it. Please help.
What you are working with is kind of scala REPL. Basically "main" function does not have any significance over there. Having said that you can run your function as follows
object Main {
def main(args: Array[String]) {
val res = for (a <- args) yield a.toUpperCase
println(res)
println("Arguments: " + res.toString)
}
}
Main.main(Array("123","23123"))
As is you can call Object Main's main method.
You can call the main method in the Main object as follows:
val args: Array[String] = Array("test1", "test2", "test3")
Main.main(args)
What you have in your main method won't print what you expect, which I assume is the values contained in the res array. To accomplish that you would need to change it to something like the following:
object Main {
def main(args: Array[String]): Unit = {
val res = for (a <- args) yield a.toUpperCase
println("Arguments: " + res.mkString(" "))
}
}
In sbt 0.13.9, I want to be able to run a task which takes in arguments from the command line and then passes those arguments on to two other tasks.
My initial attempt was something along the lines of:
lazy val logTask = InputKey[Unit](...)
lazy val runTask = InputKey[Unit](...)
lazy val TestCase = config("testCase") extend Test
runTask in TestCase := Def.inputTaskDyn {
val args: Seq[String] = spaceDelimited("<arg>").parsed
runReg(args)
}.evaluated
logTask in TestCase := Def.inputTaskDyn {
val args: Seq[String] = spaceDelimited("<arg>").parsed
log(args)
}.evaluated
def runReg(args: Seq[String]) = Def.taskDyn {
val argString = args.mkString(" ")
(logTask in TestCase).toTask(argString).value
(testOnly in TestCase).toTask(s" $argString")
}
def log(args: Seq[String]) {
(runMain in TestCase).toTask(s" LoggingClass $args.mkString(" ")")
}
But then it complains of an Illegal Dynamic Reference argString in (logTask in TestCase).toTask(argsString).value
I've also tried something like:
runTask in TestCase := {
val args: Seq[String] = spaceDelimited("<arg>").parsed
log(args).value
runReg(args).value
}
which also has an Illegal Dynamic Reference for args.
Is there any way of passing in parsed arguments into two tasks and run one after the other?
Thanks for any help.
Instead of assigning args.mkString(" ") to a variable, just pass it without assigning to any variable like below:
(logTask in TestCase).toTask(args.mkString(" ")).value
Update 1:
This kind of issues can also be sorted out with lazy initialization in sbt. So, try something like below:
lazy val argString = args.mkString(" ")
(logTask in TestCase).toTask(argString).value
How can i unit test console input in scala with scalaTest.
Code under Test:
object ConsoleAction {
def readInput(in: InputStream): List[String] = {
val bs = new BufferedSource(in)(Codec.default)
val l = bs.getLines()
l.takeWhile(_!="").toList
}
def main(args: Array[String]) {
val l = ConsoleAction.readInput(System.in)
println("--> "+l)
}
}
I'd like to test the readInput method.
A one line input can be tested like that:
"Result list" should "has 1 element" in {
val input = "Hello\\n"
val is = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))
assert(ConsoleAction.readInput(is).size===1)
}
... but what is the way for multiline input?
input line 1
input line 2
thx
Your problem lies with how you're escaping your newline. You're doing "\\n" rather than "\n". This test should pass.
"Result list" should "has 2 elements" in {
val input = "Hello\nWorld\n"
val is = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))
assert(ConsoleAction.readInput(is).size===2)
}
Given the following code:
import scala.io.Source
object Demo {
def func():Unit = {
for ( line <- Source.fromFile("C:\Users\Hen\Scala_workspace\Itay\src\Demo.scala").getLines() ) {
println(line)
}
}
def main(args: Array[String]): Unit = {
//var x=args(0).toInt;
func();
}
}
Why is the pathname marked as a compilation error?
Eclispe won't let me run it
Try triple quotes around your path, so that scala will not interpret \ + char as special characters:
"""C:\Users\Hen\Scala_workspace\Itay\src\Demo.scala"""
I am trying to write a test that will redirect the stdout of a main method, but it seems that once I call the main, it seems to start on another thread and I cannot capture the output. Here is the code:
This works:
val baos = new ByteArrayOutputStream
val ps = new PrintStream(baos)
System.setOut(ps)
print("123")
Assert.assertEquals("123", baos.toString)
This does not:
val baos = new ByteArrayOutputStream
val ps = new PrintStream(baos)
System.setOut(ps)
GameRunner.main(_)
Assert.assertEquals("123", baos.toString)
....
object GameRunner {
def main(args: Array[String]) {
print("123")
How can I catch the call to print in my test?
*I have also tried scala.Console.setOut
EDIT
I do notice that running GameRunner.main(_) does not even list anything in the console when I am not redirecting. What is causing this?
print is really Predef.print which calls Console.print. Even though you call System.setOut I don't know if that has an impact on Console.print. Try to call Console.setOut or try:
Console.withOut(ps)(GameRunner.main(null))
The other possibility is that by calling GameRunner.main(_) you are not executing anything (as may be it's just returning the function (args: Array[String]) => GameRunner.main(args)?. Should be quick to rule that out.
Edit yep:
scala> object A { def main(args: Array[String]) { println("1") } }
defined module A
scala> A.main(null)
1
scala> A.main(_)
res1: Array[String] => Unit = <function1>