scala.annotation.MainAnnotation
See theMainAnnotation companion object
trait MainAnnotation[Parser[_], Result] extends StaticAnnotation
MainAnnotation provides the functionality for a compiler-generated main class. It links a compiler-generated main method (call it compiler-main) to a user written main method (user-main). The protocol of calls from compiler-main is as follows:
- create a
command
with the command line arguments, - for each parameter of user-main, a call to
command.argGetter
, orcommand.varargGetter
if is a final varargs parameter, - a call to
command.run
with the closure of user-main applied to all arguments.
Example:
/** Sum all the numbers
*
* @param first Fist number to sum
* @param rest The rest of the numbers to sum
*/
@myMain def sum(first: Int, second: Int = 0, rest: Int*): Int = first + second + rest.sum
generates
object foo {
def main(args: Array[String]): Unit = {
val mainAnnot = new myMain()
val info = new Info(
name = "foo.main",
documentation = "Sum all the numbers",
parameters = Seq(
new Parameter("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"),
new Parameter("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum")
)
)
val mainArgsOpt = mainAnnot.command(info, args)
if mainArgsOpt.isDefined then
val mainArgs = mainArgsOpt.get
val args0 = mainAnnot.argGetter[Int](info.parameters(0), mainArgs(0), None) // using parser Int
val args1 = mainAnnot.argGetter[Int](info.parameters(1), mainArgs(1), Some(() => sum$default$1())) // using parser Int
val args2 = mainAnnot.varargGetter[Int](info.parameters(2), mainArgs.drop(2)) // using parser Int
mainAnnot.run(() => sum(args0(), args1(), args2()*))
}
}
Value parameters
- Parser
-
The class used for argument string parsing and arguments into a
T
- Result
-
The required result type of the main method. If this type is Any or Unit, any type will be accepted.
Attributes
- Companion
- object
- Experimental
- true
- Source
- MainAnnotation.scala
- Graph
-
- Supertypes
- Known subtypes
-
class newMain
Members list
In this article