1
- import annotation .StaticAnnotation
1
+ import annotation .{ Annotation , StaticAnnotation }
2
2
import collection .mutable
3
3
4
4
/** MainAnnotation provides the functionality for a compiler-generated wrapper class.
@@ -60,17 +60,15 @@ trait MainAnnotation extends StaticAnnotation:
60
60
def run (program : => MainResultType , mainName : String , docComment : String ): CommandResult
61
61
end Command
62
62
63
- // Compile-time abstractions to control wrapper class:
64
-
65
- /** The name to use for the static wrapper class
63
+ /** The fully qualified name to use for the static wrapper method
66
64
* @param mainName the fully qualified name of the user-defined main method
67
65
*/
68
- inline def wrapperClassName (mainName : String ): String
66
+ inline def wrapperName (mainName : String ): String
69
67
70
- /** The name to use for the main method in the static wrapper class
71
- * @param mainName the fully qualified name of the user-defined main method
68
+ /** An annotation type with which the wrapper method is decorated.
69
+ * No annotation is generated of the type is left abstract.
72
70
*/
73
- inline def wrapperMethodName ( mainName : String ) : String
71
+ type WrapperAnnotation <: Annotation
74
72
75
73
end MainAnnotation
76
74
@@ -137,7 +135,9 @@ class main extends MainAnnotation:
137
135
138
136
def run (f : => MainResultType , mainName : String , docComment : String ): Unit =
139
137
def usage (): Unit =
140
- println(s " Usage: java ${wrapperClassName(mainName)} ${argInfos.map(_ + _).mkString(" " )}" )
138
+ val cmd = mainName.dropRight(" .main" .length)
139
+ val params = argInfos.map(_ + _).mkString(" " )
140
+ println(s " Usage: java $cmd $params" )
141
141
142
142
def explain (): Unit =
143
143
if docComment.nonEmpty then println(docComment) // todo: process & format doc comment
@@ -168,13 +168,15 @@ class main extends MainAnnotation:
168
168
end run
169
169
end command
170
170
171
- inline def wrapperClassName (mainName : String ): String =
172
- mainName.drop(mainName.lastIndexOf('.' ) + 1 )
171
+ inline def wrapperName (mainName : String ): String =
172
+ s " ${ mainName.drop(mainName.lastIndexOf('.' ) + 1 )} .main "
173
173
174
- inline def wrapperMethodName ( mainName : String ) : String = " main "
174
+ override type WrapperAnnotation = EntryPoint
175
175
176
176
end main
177
177
178
+ class EntryPoint extends Annotation
179
+
178
180
// Sample main method
179
181
180
182
object myProgram :
@@ -188,7 +190,7 @@ end myProgram
188
190
// Compiler generated code:
189
191
190
192
object add extends main :
191
- def main (args : Array [String ]) =
193
+ @ EntryPoint def main (args : Array [String ]) =
192
194
val cmd = command(args)
193
195
val arg1 = cmd.argGetter[Int ](" num" , summon[ArgumentParser [Int ]])
194
196
val arg2 = cmd.argGetter[Int ](" inc" , summon[ArgumentParser [Int ]], Some (1 ))
0 commit comments