@@ -19,24 +19,24 @@ trait MainAnnotation extends StaticAnnotation:
19
19
type ArgumentParser [T ]
20
20
21
21
/** The required result type of the main function */
22
- type ResultType
22
+ type MainResultType
23
23
24
24
/** A new command with arguments from `args` */
25
25
def command (args : Array [String ]): Command
26
26
27
27
/** A class representing a command to run */
28
28
abstract class Command :
29
29
30
- /** The getter for the next simple argument */
30
+ /** The getter for the next argument of type `T` */
31
31
def argGetter [T ](argName : String , fromString : ArgumentParser [T ], defaultValue : Option [T ] = None ): () => T
32
32
33
- /** The getter for a final varargs argument */
34
- def argsGetter [T ](argName : String , fromString : ArgumentParser [T ]): () => List [T ]
33
+ /** The getter for a final varargs argument of type `T*` */
34
+ def argsGetter [T ](argName : String , fromString : ArgumentParser [T ]): () => Seq [T ]
35
35
36
- /** Run function `f() ` if all arguments are valid,
36
+ /** Run `program ` if all arguments are valid,
37
37
* or print usage information and/or error messages.
38
38
*/
39
- def run (f : => ResultType , progName : String , docComment : String ): Unit
39
+ def run (program : => MainResultType , progName : String , docComment : String ): Unit
40
40
end Command
41
41
end MainAnnotation
42
42
@@ -45,7 +45,7 @@ end MainAnnotation
45
45
class main extends MainAnnotation :
46
46
47
47
type ArgumentParser [T ] = util.FromString [T ]
48
- type ResultType = Any
48
+ type MainResultType = Any
49
49
50
50
def command (args : Array [String ]): Command = new Command :
51
51
@@ -54,29 +54,29 @@ class main extends MainAnnotation:
54
54
* "*" if it is a vararg
55
55
* "" otherwise
56
56
*/
57
- var argInfos = new mutable.ListBuffer [(String , String )]
57
+ private var argInfos = new mutable.ListBuffer [(String , String )]
58
58
59
59
/** A buffer for all errors */
60
- var errors = new mutable.ListBuffer [String ]
60
+ private var errors = new mutable.ListBuffer [String ]
61
61
62
62
/** Issue an error, and return an uncallable getter */
63
- def error (msg : String ): () => Nothing =
63
+ private def error (msg : String ): () => Nothing =
64
64
errors += msg
65
65
() => assertFail(" trying to get invalid argument" )
66
66
67
67
/** The next argument index */
68
- var argIdx : Int = 0
68
+ private var argIdx : Int = 0
69
69
70
- def argAt (idx : Int ): Option [String ] =
70
+ private def argAt (idx : Int ): Option [String ] =
71
71
if idx < args.length then Some (args(idx)) else None
72
72
73
- def nextPositionalArg (): Option [String ] =
73
+ private def nextPositionalArg (): Option [String ] =
74
74
while argIdx < args.length && args(argIdx).startsWith(" --" ) do argIdx += 2
75
75
val result = argAt(argIdx)
76
76
argIdx += 1
77
77
result
78
78
79
- def convert [T ](argName : String , arg : String , p : ArgumentParser [T ]): () => T =
79
+ private def convert [T ](argName : String , arg : String , p : ArgumentParser [T ]): () => T =
80
80
p.fromStringOption(arg) match
81
81
case Some (t) => () => t
82
82
case None => error(s " invalid argument for $argName: $arg" )
@@ -91,15 +91,15 @@ class main extends MainAnnotation:
91
91
case Some (t) => () => t
92
92
case None => error(s " missing argument for $argName" )
93
93
94
- def argsGetter [T ](argName : String , fromString : ArgumentParser [T ]): () => List [T ] =
94
+ def argsGetter [T ](argName : String , p : ArgumentParser [T ]): () => Seq [T ] =
95
95
argInfos += ((argName, " *" ))
96
- def recur (): List [() => T ] = nextPositionalArg() match
97
- case Some (arg) => convert(arg, argName, fromString ) :: recur ()
96
+ def remainingArgGetters (): List [() => T ] = nextPositionalArg() match
97
+ case Some (arg) => convert(arg, argName, p ) :: remainingArgGetters ()
98
98
case None => Nil
99
- val fns = recur ()
100
- () => fns .map(_())
99
+ val getters = remainingArgGetters ()
100
+ () => getters .map(_())
101
101
102
- def run (f : => ResultType , progName : String , docComment : String ): Unit =
102
+ def run (f : => MainResultType , progName : String , docComment : String ): Unit =
103
103
def usage (): Unit =
104
104
println(s " Usage: $progName ${argInfos.map(_ + _).mkString(" " )}" )
105
105
0 commit comments