|
| 1 | +package scala.annotation |
| 2 | + |
| 3 | +/** MainAnnotation provides the functionality for a compiler-generated main class. |
| 4 | + * It links a compiler-generated main method (call it compiler-main) to a user |
| 5 | + * written main method (user-main). |
| 6 | + * The protocol of calls from compiler-main is as follows: |
| 7 | + * |
| 8 | + * - create a `command` with the command line arguments, |
| 9 | + * - for each parameter of user-main, a call to `command.argGetter`, |
| 10 | + * or `command.varargGetter` if is a final varargs parameter, |
| 11 | + * - a call to `command.run` with the closure of user-main applied to all arguments. |
| 12 | + * |
| 13 | + * Example: |
| 14 | + * ```scala |
| 15 | + * /** Sum all the numbers |
| 16 | + * * |
| 17 | + * * @param first Fist number to sum |
| 18 | + * * @param rest The rest of the numbers to sum |
| 19 | + * */ |
| 20 | + * @myMain def sum(first: Int, second: Int = 0, rest: Int*): Int = first + second + rest.sum |
| 21 | + * ``` |
| 22 | + * generates |
| 23 | + * ```scala |
| 24 | + * object foo { |
| 25 | + * def main(args: Array[String]): Unit = { |
| 26 | + * val mainAnnot = new myMain() |
| 27 | + * val info = new Info( |
| 28 | + * name = "foo.main", |
| 29 | + * documentation = "Sum all the numbers", |
| 30 | + * parameters = Seq( |
| 31 | + * new Parameter("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"), |
| 32 | + * new Parameter("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum") |
| 33 | + * ) |
| 34 | + * ) |
| 35 | + * val mainArgsOpt = mainAnnot.command(info, args) |
| 36 | + * if mainArgsOpt.isDefined then |
| 37 | + * val mainArgs = mainArgsOpt.get |
| 38 | + * val args0 = mainAnnot.argGetter[Int](info.parameters(0), mainArgs(0), None) // using parser Int |
| 39 | + * val args1 = mainAnnot.argGetter[Int](info.parameters(1), mainArgs(1), Some(() => sum$default$1())) // using parser Int |
| 40 | + * val args2 = mainAnnot.varargGetter[Int](info.parameters(2), mainArgs.drop(2)) // using parser Int |
| 41 | + * mainAnnot.run(() => sum(args0(), args1(), args2()*)) |
| 42 | + * } |
| 43 | + * } |
| 44 | + * ``` |
| 45 | + * |
| 46 | + * @param Parser The class used for argument string parsing and arguments into a `T` |
| 47 | + * @param Result The required result type of the main method. |
| 48 | + * If this type is Any or Unit, any type will be accepted. |
| 49 | + */ |
| 50 | +@experimental |
| 51 | +trait MainAnnotation[Parser[_], Result] extends StaticAnnotation: |
| 52 | + import MainAnnotation.{Info, Parameter} |
| 53 | + |
| 54 | + /** Process the command arguments before parsing them. |
| 55 | + * |
| 56 | + * Return `Some` of the sequence of arguments that will be parsed to be passed to the main method. |
| 57 | + * This sequence needs to have the same length as the number of parameters of the main method (i.e. `info.parameters.size`). |
| 58 | + * If there is a varags parameter, then the sequence must be at least of length `info.parameters.size - 1`. |
| 59 | + * |
| 60 | + * Returns `None` if the arguments are invalid and parsing and run should be stopped. |
| 61 | + * |
| 62 | + * @param info The information about the command (name, documentation and info about parameters) |
| 63 | + * @param args The command line arguments |
| 64 | + */ |
| 65 | + def command(info: Info, args: Seq[String]): Option[Seq[String]] |
| 66 | + |
| 67 | + /** The getter for the `idx`th argument of type `T` |
| 68 | + * |
| 69 | + * @param idx The index of the argument |
| 70 | + * @param defaultArgument Optional lambda to instantiate the default argument |
| 71 | + */ |
| 72 | + def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using Parser[T]): () => T |
| 73 | + |
| 74 | + /** The getter for a final varargs argument of type `T*` */ |
| 75 | + def varargGetter[T](param: Parameter, args: Seq[String])(using Parser[T]): () => Seq[T] |
| 76 | + |
| 77 | + /** Run `program` if all arguments are valid if all arguments are valid |
| 78 | + * |
| 79 | + * @param program A function containing the call to the main method and instantiation of its arguments |
| 80 | + */ |
| 81 | + def run(program: () => Result): Unit |
| 82 | + |
| 83 | +end MainAnnotation |
| 84 | + |
| 85 | +@experimental |
| 86 | +object MainAnnotation: |
| 87 | + |
| 88 | + /** Information about the main method |
| 89 | + * |
| 90 | + * @param name The name of the main method |
| 91 | + * @param documentation The documentation of the main method without the `@param` documentation (see Parameter.documentaion) |
| 92 | + * @param parameters Information about the parameters of the main method |
| 93 | + */ |
| 94 | + final class Info( |
| 95 | + val name: String, |
| 96 | + val documentation: String, |
| 97 | + val parameters: Seq[Parameter], |
| 98 | + ): |
| 99 | + |
| 100 | + /** If the method ends with a varargs parameter */ |
| 101 | + def hasVarargs: Boolean = parameters.nonEmpty && parameters.last.isVarargs |
| 102 | + |
| 103 | + end Info |
| 104 | + |
| 105 | + /** Information about a parameter of a main method |
| 106 | + * |
| 107 | + * @param name The name of the parameter |
| 108 | + * @param typeName The name of the parameter's type |
| 109 | + * @param hasDefault If the parameter has a default argument |
| 110 | + * @param isVarargs If the parameter is a varargs parameter (can only be true for the last parameter) |
| 111 | + * @param documentation The documentation of the parameter (from `@param` documentation in the main method) |
| 112 | + * @param annotations The annotations of the parameter that extend `ParameterAnnotation` |
| 113 | + */ |
| 114 | + final class Parameter( |
| 115 | + val name: String, |
| 116 | + val typeName: String, |
| 117 | + val hasDefault: Boolean, |
| 118 | + val isVarargs: Boolean, |
| 119 | + val documentation: String, |
| 120 | + val annotations: Seq[ParameterAnnotation], |
| 121 | + ) |
| 122 | + |
| 123 | + /** Marker trait for annotations that will be included in the Parameter annotations. */ |
| 124 | + trait ParameterAnnotation extends StaticAnnotation |
| 125 | + |
| 126 | +end MainAnnotation |
0 commit comments