@@ -52,60 +52,11 @@ import annotation._
52
52
* - `scala foo -x 1 -s abc`
53
53
* - `scala foo --number 1 --explanation abc`
54
54
* - `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
57
55
*/
58
- final class main ( maxLineLength : Int ) extends MainAnnotation :
56
+ final class main extends MainAnnotation :
59
57
import main ._
60
58
import MainAnnotation ._
61
59
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
-
109
60
override type ArgumentParser [T ] = util.CommandLineParser .FromString [T ]
110
61
override type MainResultType = Any
111
62
@@ -118,6 +69,8 @@ final class main(maxLineLength: Int) extends MainAnnotation:
118
69
private val argMarker = " --"
119
70
private val shortArgMarker = " -"
120
71
72
+ private val maxUsageLineLength = 120
73
+
121
74
/** A map from argument canonical name (the name of the parameter in the method definition) to parameter informations */
122
75
private val nameToParameterInfos : Map [String , ParameterInfos ] = parameterInfoss.map(infos => infos.name -> infos).toMap
123
76
@@ -230,7 +183,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
230
183
231
184
val usageBeginning = s " Usage: $commandName "
232
185
val argsOffset = usageBeginning.length
233
- val usages = wrapArgumentUsages(argsUsage, maxLineLength - argsOffset)
186
+ val usages = wrapArgumentUsages(argsUsage, maxUsageLineLength - argsOffset)
234
187
235
188
println(usageBeginning + usages.mkString(" \n " + " " * argsOffset))
236
189
end usage
@@ -252,7 +205,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
252
205
}
253
206
254
207
if (documentation.nonEmpty)
255
- println(wrapLongLine(documentation, maxLineLength ).mkString(" \n " ))
208
+ println(wrapLongLine(documentation, maxUsageLineLength ).mkString(" \n " ))
256
209
if (nameToParameterInfos.nonEmpty) {
257
210
val argNameShift = 2
258
211
val argDocShift = argNameShift + 2
@@ -279,7 +232,7 @@ final class main(maxLineLength: Int) extends MainAnnotation:
279
232
doc => if (doc.nonEmpty) {
280
233
val shiftedDoc =
281
234
doc.split(" \n " ).nn
282
- .map(line => shiftLines(wrapLongLine(line.nn, maxLineLength - argDocShift), argDocShift))
235
+ .map(line => shiftLines(wrapLongLine(line.nn, maxUsageLineLength - argDocShift), argDocShift))
283
236
.mkString(" \n " )
284
237
argDoc.append(" \n " ).append(shiftedDoc)
285
238
}
0 commit comments