@@ -7,22 +7,56 @@ package scala.annotation
7
7
*
8
8
* - create a `command` with the command line arguments,
9
9
* - for each parameter of user-main, a call to `command.argGetter`,
10
- * or `command.argsGetter ` if is a final varargs parameter,
10
+ * or `command.varargGetter ` if is a final varargs parameter,
11
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, rest: Int*): Int = first + rest.sum
21
+ * ```
22
+ * generates
23
+ * ```scala
24
+ * object foo {
25
+ * def main(args: Array[String]): Unit = {
26
+ * val cmd = new myMain().command(
27
+ * args = args,
28
+ * commandName = "sum",
29
+ * documentation = "Sum all the numbers",
30
+ * new ParameterInfo("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"),
31
+ * new ParameterInfo("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum")
32
+ * )
33
+ * val args0 = cmd.argGetter[Int](0, None) // using cmd.Parser[Int]
34
+ * val args1 = cmd.varargGetter[Int] // using cmd.Parser[Int]
35
+ * cmd.run(() => sum(args0(), args1()*))
36
+ * }
37
+ * }
38
+ * ```
12
39
*/
13
40
@ experimental
14
41
trait MainAnnotation extends StaticAnnotation :
15
42
import MainAnnotation .*
16
43
17
- /** The class used for argument string parsing. E.g. `scala.util.CommandLineParser.FromString`,
18
- * but could be something else
19
- */
44
+ /** The class used for argument string parsing and arguments into a `T` */
20
45
type Parser [T ]
21
46
22
- /** The required result type of the main function */
47
+ /** The required result type of the main method.
48
+ *
49
+ * If this type is Any or Unit, any type will be accepted.
50
+ */
23
51
type Result
24
52
25
- /** A new command with arguments from `args` */
53
+ /** A new command with arguments from `args`
54
+ *
55
+ * @param args The command line arguments
56
+ * @param commandName The name of the command (name of the annotated method)
57
+ * @param documentation The documentation of the command (without the `@param` documentation)
58
+ * @param parameterInfos Information about all the parameters (name, type, has default, is varargs and documentation)
59
+ */
26
60
def command (args : Array [String ], commandName : String , documentation : String , parameterInfos : ParameterInfo * ): Command [Parser , Result ]
27
61
28
62
end MainAnnotation
@@ -39,7 +73,14 @@ object MainAnnotation:
39
73
paramAnnotations : Seq [ParameterAnnotation ],
40
74
) {
41
75
42
- /** ParameterInfo with a name, the type of the parameter and if it has a default */
76
+ /** ParameterInfo with a name, the type of the parameter and if it has a default
77
+ *
78
+ * @param name The name of the parameter
79
+ * @param typeName The type of the parameter
80
+ * @param hasDefault If the parameter has a default argument
81
+ * @param isVarargs If the parameter is a varargs parameter (can only be true for the last parameter)
82
+ * @param documentation The documentation of the parameter (from `@param` documentation in the main method)
83
+ */
43
84
def this (name : String , typeName : String , hasDefault : Boolean , isVarargs : Boolean , documentation : String ) =
44
85
this (name, typeName, hasDefault, isVarargs, documentation, Seq .empty)
45
86
@@ -49,7 +90,7 @@ object MainAnnotation:
49
90
/** The name of the parameter's type */
50
91
def typeName : String = paramTypeName
51
92
52
- /** If the parameter has a default value */
93
+ /** If the parameter has a default argument */
53
94
def hasDefault : Boolean = paramHasDefault
54
95
55
96
/** If this is a varargs parameter. Can only be true if it is the last parameter. */
@@ -71,14 +112,19 @@ object MainAnnotation:
71
112
/** A class representing a command to run */
72
113
trait Command [Parser [_], Result ]:
73
114
74
- /** The getter for the `idx`th argument of type `T` */
115
+ /** The getter for the `idx`th argument of type `T`
116
+ *
117
+ * @param idx The index of the argument
118
+ * @param defaultArgument Optional lambda to instantiate the default argument
119
+ */
75
120
def argGetter [T ](idx : Int , defaultArgument : Option [() => T ])(using Parser [T ]): () => T
76
121
77
122
/** The getter for a final varargs argument of type `T*` */
78
123
def varargGetter [T ](using Parser [T ]): () => Seq [T ]
79
124
80
- /** Run `program` if all arguments are valid,
81
- * or print usage information and/or error messages.
125
+ /** Run `program` if all arguments are valid if all arguments are valid
126
+ *
127
+ * @param program A function containing the call to the main method and instantiation of its arguments
82
128
*/
83
129
def run (program : () => Result ): Unit
84
130
end Command
0 commit comments