@@ -79,14 +79,6 @@ final class newMain extends MainAnnotation[FromString, Any]:
79
79
80
80
private var info : Info = _ // TODO remove this var
81
81
82
- /** A buffer for all errors */
83
- private val errors = new mutable.ArrayBuffer [String ]
84
-
85
- /** Issue an error, and return an uncallable getter */
86
- private def error (msg : String ): () => Nothing =
87
- errors += msg
88
- () => throw new AssertionError (" trying to get invalid argument" )
89
-
90
82
private def getAliases (param : Parameter ): Seq [String ] =
91
83
param.annotations.collect{ case a : Alias => a }.flatMap(_.aliases)
92
84
@@ -117,6 +109,12 @@ final class newMain extends MainAnnotation[FromString, Any]:
117
109
def command (info : Info , args : Seq [String ]): Option [Seq [String ]] =
118
110
this .info = info
119
111
112
+ val errors = new mutable.ArrayBuffer [String ]
113
+
114
+ def error (msg : String ): Unit = {
115
+ errors += msg
116
+ }
117
+
120
118
val namesToCanonicalName : Map [String , String ] = info.parameters.flatMap(
121
119
infos =>
122
120
val names = getAlternativeNames(infos)
@@ -316,24 +314,33 @@ final class newMain extends MainAnnotation[FromString, Any]:
316
314
317
315
def argGetter [T ](param : Parameter , arg : String , defaultArgument : Option [() => T ])(using p : FromString [T ]): () => T = {
318
316
if arg.nonEmpty then parse[T ](param, arg)
319
- else defaultArgument match
320
- case Some (defaultGetter) => defaultGetter
321
- case None => error(s " missing argument for ${param.name}" )
317
+ else
318
+ assert(param.hasDefault)
319
+
320
+ defaultArgument.get
322
321
}
323
322
324
323
def varargGetter [T ](param : Parameter , args : Seq [String ])(using p : FromString [T ]): () => Seq [T ] = {
325
324
val getters = args.map(arg => parse[T ](param, arg))
326
325
() => getters.map(_())
327
326
}
328
327
328
+ /** A buffer for all parsing errors errors */
329
+ private lazy val parseErrors = new mutable.ArrayBuffer [String ]
330
+
331
+ /** Issue an error, and return an uncallable getter */
332
+ private def parseError (msg : String ): () => Nothing =
333
+ parseErrors += msg
334
+ () => throw new AssertionError (" trying to get invalid argument" )
335
+
329
336
private def parse [T ](param : Parameter , arg : String )(using p : FromString [T ]): () => T =
330
337
p.fromStringOption(arg) match
331
338
case Some (t) => () => t
332
- case None => error (s " could not parse argument for ` ${param.name}` of type ${param.typeName.split('.' ).last}: $arg" )
339
+ case None => parseError (s " could not parse argument for ` ${param.name}` of type ${param.typeName.split('.' ).last}: $arg" )
333
340
334
341
def run (execProgram : () => Any ): Unit = {
335
- if errors .nonEmpty then
336
- for msg <- errors do println(s " Error: $msg" )
342
+ if parseErrors .nonEmpty then
343
+ for msg <- parseErrors do println(s " Error: $msg" )
337
344
usage()
338
345
else
339
346
execProgram()
0 commit comments