|
MethodTasks
sbt has movedsbt has now completely moved to GitHub. See https://github.com/harrah/xsbt/wiki/Input-Tasks. Method TasksAs shown in several examples on the Triggered Execution page, a method task accepts an array of arguments and creates the task to be run from those arguments. Usage of a method task from the interactive prompt looks like: > run 10000 5 0.3 or > test-only example.TestSpecification Defining your own method task is similar to defining a normal action, as shown in the following example. lazy val example =
task { args =>
if(args.length == 2)
actionConstructor(args(0).toInt, args(1))
else
task { Some("Usage: example <integer> <string>") }
}
def actionConstructor(a: Int, b: String) =
task {
println("Arguments were: " + a + " and " + b)
None
}The type of args is Array[String]. You can then call example like: > example 5 text Arguments were: 5 and text Additionally, you can use quotes to include whitespace in arguments. Quotes must be escaped (\") and so must backslashes (\\): > example "5" "Text with \"spaces\" and a backslash \\ included." Arguments were: 5 and Text with "spaces" and a backslash \ included. You can provide strings for basic tab completion for method tasks using the completeWith method. def testNames: Seq[String] = ...
lazy val myTestOnly =
task { args => ... } completeWith( testNames )The argument to completeWith is call-by-name and is re-evaluated after a command is run. In a multi-project setting, method tasks can only be run from the project they are defined on. | |
It should be noted that these arguments either can come in from the console in the way you'd expect (as shown in the example), or from the command line IF you put quotes around the task and your arguments, i.e. you can do $sbt <CR> >example 5 text, or $sbt "example 5 text". This drove me mad trying to figure out why my code wasn't working, and its strange that its this way anyway. It should "just work".