@@ -24,11 +24,15 @@ package scala.annotation
2424 * object foo {
2525 * def main(args: Array[String]): Unit = {
2626 * 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")
27+ * info = new CommandInfo(
28+ * name = "foo.main",
29+ * documentation = "Sum all the numbers",
30+ * parameters = Seq(
31+ * new ParameterInfo("first", "scala.Int", hasDefault=false, isVarargs=false, "Fist number to sum"),
32+ * new ParameterInfo("rest", "scala.Int" , hasDefault=false, isVarargs=true, "The rest of the numbers to sum")
33+ * )
34+ * )
35+ * args = args
3236 * )
3337 * val args0 = cmd.argGetter[Int](0, None) // using cmd.Parser[Int]
3438 * val args1 = cmd.varargGetter[Int] // using cmd.Parser[Int]
@@ -52,18 +56,38 @@ trait MainAnnotation extends StaticAnnotation:
5256
5357 /** A new command with arguments from `args`
5458 *
59+ * @param info The information about the command (name, documentation and info about parameters)
5560 * @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)
5961 */
60- def command (args : Array [ String ], commandName : String , documentation : String , parameterInfos : ParameterInfo * ): Command [Parser , Result ]
62+ def command (info : CommandInfo , args : Array [ String ] ): Command [Parser , Result ]
6163
6264end MainAnnotation
6365
6466@ experimental
6567object MainAnnotation :
6668
69+ /** A class representing a command to run */
70+ trait Command [Parser [_], Result ]:
71+
72+ /** The getter for the `idx`th argument of type `T`
73+ *
74+ * @param idx The index of the argument
75+ * @param defaultArgument Optional lambda to instantiate the default argument
76+ */
77+ def argGetter [T ](idx : Int , defaultArgument : Option [() => T ])(using Parser [T ]): () => T
78+
79+ /** The getter for a final varargs argument of type `T*` */
80+ def varargGetter [T ](using Parser [T ]): () => Seq [T ]
81+
82+ /** Run `program` if all arguments are valid if all arguments are valid
83+ *
84+ * @param program A function containing the call to the main method and instantiation of its arguments
85+ */
86+ def run (program : () => Result ): Unit
87+ end Command
88+
89+ final class CommandInfo (val name : String , val documentation : String , val parameters : Seq [ParameterInfo ])
90+
6791 /** ParameterInfo with a name, the type of the parameter and if it has a default
6892 *
6993 * @param name The name of the parameter
@@ -79,7 +103,7 @@ object MainAnnotation:
79103 paramIsVarargs : Boolean ,
80104 paramDocumentation : String ,
81105 paramAnnotations : Seq [ParameterAnnotation ],
82- ) {
106+ ):
83107
84108 /** The name of the parameter */
85109 def name : String = paramName
@@ -100,27 +124,10 @@ object MainAnnotation:
100124 def annotations : Seq [ParameterAnnotation ] = paramAnnotations
101125
102126 override def toString : String = s " $name: $typeName"
103- }
104127
105- /** A class representing a command to run */
106- trait Command [Parser [_], Result ]:
128+ end ParameterInfo
107129
108- /** The getter for the `idx`th argument of type `T`
109- *
110- * @param idx The index of the argument
111- * @param defaultArgument Optional lambda to instantiate the default argument
112- */
113- def argGetter [T ](idx : Int , defaultArgument : Option [() => T ])(using Parser [T ]): () => T
114130
115- /** The getter for a final varargs argument of type `T*` */
116- def varargGetter [T ](using Parser [T ]): () => Seq [T ]
117-
118- /** Run `program` if all arguments are valid if all arguments are valid
119- *
120- * @param program A function containing the call to the main method and instantiation of its arguments
121- */
122- def run (program : () => Result ): Unit
123- end Command
124131
125132 /** Marker trait for annotations that will be included in the ParameterInfo annotations. */
126133 trait ParameterAnnotation extends StaticAnnotation
0 commit comments