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.
- Companion:
- object
- Source:
- MainAnnotation.scala
Value members
Abstract methods
The getter for the idx
th argument of type T
The getter for the idx
th argument of type T
- Value parameters:
- defaultArgument
Optional lambda to instantiate the default argument
- idx
The index of the argument
- Source:
- MainAnnotation.scala
Process the command arguments before parsing them.
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:
- args
The command line arguments
- info
The information about the command (name, documentation and info about parameters)
- Source:
- MainAnnotation.scala
Run program
if all arguments are valid if all arguments are valid
Run program
if all arguments are valid if all arguments are valid
- Value parameters:
- program
A function containing the call to the main method and instantiation of its arguments
- Source:
- MainAnnotation.scala
The getter for a final varargs argument of type T*
The getter for a final varargs argument of type T*
- Source:
- MainAnnotation.scala