@@ -52,60 +52,11 @@ import annotation._
5252 * - `scala foo -x 1 -s abc`
5353 * - `scala foo --number 1 --explanation abc`
5454 * - `scala foo -x 1 --explanation abc`
55- *
56- * @param maxLineLength the maximum number of characters to print on a single line when displaying the help
5755 */
58- final class main ( maxLineLength : Int ) extends MainAnnotation :
56+ final class main extends MainAnnotation :
5957 import main ._
6058 import MainAnnotation ._
6159
62- /**
63- * The annotation that designates a main function.
64- * Main functions are entry points for Scala programs. They can be called through a command line interface by using
65- * the `scala` command, followed by their name and, optionally, their parameters.
66- *
67- * The parameters of a main function may have any type `T`, as long as there exists a
68- * `given util.CommandLineParser.FromString[T]` in the scope. It will be used for parsing the string given as input
69- * into the correct argument type.
70- * These types already have parsers defined:
71- * - String,
72- * - Boolean,
73- * - Byte, Short, Int, Long, Float, Double.
74- *
75- * The parameters of a main function may be passed either by position, or by name. Passing an argument positionaly
76- * means that you give the arguments in the same order as the function's signature. Passing an argument by name means
77- * that you give the argument right after giving its name. Considering the function
78- * `@main def foo(i: Int, s: String)`, we may have arguments passed:
79- * - by position: `scala foo 1 abc`,
80- * - by name: `scala foo --i 1 --s abc` or `scala foo --s abc --i 1`.
81- *
82- * A mixture of both is also possible: `scala foo --s abc 1` is equivalent to all previous examples.
83- *
84- * Note that main function overloading is not currently supported, i.e. you cannot define two main methods that have
85- * the same name in the same project.
86- *
87- * A special argument is used to display help regarding a main function: `--help`. If used as argument, the program
88- * will display some useful information about the main function. This help directly uses the ScalaDoc comment
89- * associated with the function, more precisely its description and the description of the parameters documented with
90- * `@param`.
91- *
92- *
93- * Parameters may be given annotations to add functionalities to the main function:
94- * - `main.ShortName` adds a short name to a parameter. For example, if a parameter `node` has as short name `n`, it
95- * may be addressed using either `--node` or `-n`,
96- * - `main.Name` adds another name to a parameter. For example, if a parameter `node` has as alternative name
97- * `otherNode`, it may be addressed using either `--node` or `--otherNode`.
98- *
99- * Here is an example of a main function with annotated parameters:
100- * `@main def foo(@main.ShortName('x') number: Int, @main.Name("explanation") s: String)`. The following commands are
101- * equivalent:
102- * - `scala foo --number 1 --s abc`
103- * - `scala foo -x 1 --s abc`
104- * - `scala foo --number 1 --explanation abc`
105- * - `scala foo -x 1 --explanation abc`
106- */
107- def this () = this (120 )
108-
10960 override type ArgumentParser [T ] = util.CommandLineParser .FromString [T ]
11061 override type MainResultType = Any
11162
@@ -118,6 +69,8 @@ final class main(maxLineLength: Int) extends MainAnnotation:
11869 private val argMarker = " --"
11970 private val shortArgMarker = " -"
12071
72+ private val maxUsageLineLength = 120
73+
12174 /** A map from argument canonical name (the name of the parameter in the method definition) to parameter informations */
12275 private val nameToParameterInfos : Map [String , ParameterInfos ] = parameterInfoss.map(infos => infos.name -> infos).toMap
12376
@@ -230,7 +183,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
230183
231184 val usageBeginning = s " Usage: $commandName "
232185 val argsOffset = usageBeginning.length
233- val usages = wrapArgumentUsages(argsUsage, maxLineLength - argsOffset)
186+ val usages = wrapArgumentUsages(argsUsage, maxUsageLineLength - argsOffset)
234187
235188 println(usageBeginning + usages.mkString(" \n " + " " * argsOffset))
236189 end usage
@@ -252,7 +205,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
252205 }
253206
254207 if (documentation.nonEmpty)
255- println(wrapLongLine(documentation, maxLineLength ).mkString(" \n " ))
208+ println(wrapLongLine(documentation, maxUsageLineLength ).mkString(" \n " ))
256209 if (nameToParameterInfos.nonEmpty) {
257210 val argNameShift = 2
258211 val argDocShift = argNameShift + 2
@@ -279,7 +232,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
279232 doc => if (doc.nonEmpty) {
280233 val shiftedDoc =
281234 doc.split(" \n " ).nn
282- .map(line => shiftLines(wrapLongLine(line.nn, maxLineLength - argDocShift), argDocShift))
235+ .map(line => shiftLines(wrapLongLine(line.nn, maxUsageLineLength - argDocShift), argDocShift))
283236 .mkString(" \n " )
284237 argDoc.append(" \n " ).append(shiftedDoc)
285238 }
0 commit comments