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:
command with the command line arguments,command.argGetter, or command.varargGetter if is a final varargs parameter,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 |
|
|---|---|
| Supertypes | |
| Known subtypes |
The getter for the idxth argument of type T
| Value parameters |
|
|---|
Process the command arguments before parsing them.
Return Some of the sequence of arguments that will be parsed to be passed to the main method. This sequence needs to have the same length as the number of parameters of the main method (i.e. info.parameters.size). If there is a varags parameter, then the sequence must be at least of length info.parameters.size - 1.
Returns None if the arguments are invalid and parsing and run should be stopped.
| Value parameters |
|
|---|
Run program if all arguments are valid if all arguments are valid
| Value parameters |
|
|---|
The getter for a final varargs argument of type T*
© 2002-2022 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://scala-lang.org/api/3.2.0/scala/annotation/MainAnnotation.html