Skip to content

Commit f2339d7

Browse files
committed
Add documentation
1 parent e786afb commit f2339d7

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

library/src/scala/annotation/MainAnnotation.scala

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,56 @@ package scala.annotation
77
*
88
* - create a `command` with the command line arguments,
99
* - 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,
1111
* - 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+
* ```
1239
*/
1340
@experimental
1441
trait MainAnnotation extends StaticAnnotation:
1542
import MainAnnotation.*
1643

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` */
2045
type Parser[T]
2146

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+
*/
2351
type Result
2452

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+
*/
2660
def command(args: Array[String], commandName: String, documentation: String, parameterInfos: ParameterInfo*): Command[Parser, Result]
2761

2862
end MainAnnotation
@@ -39,7 +73,14 @@ object MainAnnotation:
3973
paramAnnotations: Seq[ParameterAnnotation],
4074
) {
4175

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+
*/
4384
def this(name: String, typeName: String, hasDefault: Boolean, isVarargs: Boolean, documentation: String) =
4485
this(name, typeName, hasDefault, isVarargs, documentation, Seq.empty)
4586

@@ -49,7 +90,7 @@ object MainAnnotation:
4990
/** The name of the parameter's type */
5091
def typeName: String = paramTypeName
5192

52-
/** If the parameter has a default value */
93+
/** If the parameter has a default argument */
5394
def hasDefault: Boolean = paramHasDefault
5495

5596
/** If this is a varargs parameter. Can only be true if it is the last parameter. */
@@ -71,14 +112,19 @@ object MainAnnotation:
71112
/** A class representing a command to run */
72113
trait Command[Parser[_], Result]:
73114

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+
*/
75120
def argGetter[T](idx: Int, defaultArgument: Option[() => T])(using Parser[T]): () => T
76121

77122
/** The getter for a final varargs argument of type `T*` */
78123
def varargGetter[T](using Parser[T]): () => Seq[T]
79124

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
82128
*/
83129
def run(program: () => Result): Unit
84130
end Command

0 commit comments

Comments
 (0)