Skip to content

Commit 64ec37e

Browse files
committed
Pass ParamInfo to argGetter and varargGetter
1 parent fbdb056 commit 64ec37e

12 files changed

+36
-35
lines changed

compiler/src/dotty/tools/dotc/ast/MainProxies.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ object MainProxies {
275275
Apply(ref(defn.SomeClass.companionModule.termRef), value)
276276
val argGetter0 = TypeApply(Select(Ident(nme.annotation), getterName), TypeTree(formalType) :: Nil)
277277
val index = Literal(Constant(idx))
278+
val paramInfo = Apply(Select(Ident(nme.info), nme.parameters), index)
278279
val argGetter =
279-
if isRepeated then Apply(argGetter0, List(Apply(Select(Ident(nme.cmd), nme.drop), List(index))))
280-
else Apply(argGetter0, List(index, Apply(Ident(nme.cmd), List(index)), defaultValueGetterOpt))
280+
if isRepeated then Apply(argGetter0, List(paramInfo, Apply(Select(Ident(nme.cmd), nme.drop), List(index))))
281+
else Apply(argGetter0, List(paramInfo, Apply(Ident(nme.cmd), List(index)), defaultValueGetterOpt))
281282
ValDef(argName, TypeTree(), argGetter)
282283
end argValDefs
283284

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ object StdNames {
565565
val ordinalDollar: N = "$ordinal"
566566
val ordinalDollar_ : N = "_$ordinal"
567567
val origin: N = "origin"
568+
val parameters: N = "parameters"
568569
val parts: N = "parts"
569570
val postfixOps: N = "postfixOps"
570571
val prefix : N = "prefix"

docs/_docs/reference/experimental/main-annotation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ object foo {
3131
val mainArgsOpt = mainAnnot.command(info, args)
3232
if mainArgsOpt.isDefined then
3333
val mainArgs = mainArgsOpt.get
34-
val args0 = mainAnnot.argGetter[Int](0, mainArgs(0), None) // using a parser of Int
35-
val args1 = mainAnnot.argGetter[Int](1, mainArgs(1), Some(() => sum$default$1())) // using a parser of Int
36-
val args2 = mainAnnot.varargGetter[Int](mainArgs.drop(2)) // using a parser of Int
34+
val args0 = mainAnnot.argGetter[Int](info.parameters(0), mainArgs(0), None) // using a parser of Int
35+
val args1 = mainAnnot.argGetter[Int](info.parameters(1), mainArgs(1), Some(() => sum$default$1())) // using a parser of Int
36+
val args2 = mainAnnot.varargGetter[Int](info.parameters(2), mainArgs.drop(2)) // using a parser of Int
3737
mainAnnot.run(() => sum(args0(), args1(), args2()*))
3838
}
3939
}
@@ -79,10 +79,10 @@ import scala.util.CommandLineParser.FromString[T]
7979
else
8080
Some(args)
8181

82-
def argGetter[T](arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
82+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
8383
() => parser.fromString(arg)
8484

85-
def varargGetter[T](args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
85+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
8686
() => args.map(arg => parser.fromString(arg))
8787

8888
def run(program: () => Int): Unit =

library/src/scala/annotation/MainAnnotation.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ package scala.annotation
3535
* val mainArgsOpt = mainAnnot.command(info, args)
3636
* if mainArgsOpt.isDefined then
3737
* val mainArgs = mainArgsOpt.get
38-
* val args0 = mainAnnot.argGetter[Int](0, mainArgs(0), None) // using parser Int
39-
* val args1 = mainAnnot.argGetter[Int](1, mainArgs(1), Some(() => sum$default$1())) // using parser Int
40-
* val args2 = mainAnnot.varargGetter[Int](mainArgs.drop(2)) // using parser Int
38+
* val args0 = mainAnnot.argGetter[Int](info.parameters(0), mainArgs(0), None) // using parser Int
39+
* val args1 = mainAnnot.argGetter[Int](info.parameters(1), mainArgs(1), Some(() => sum$default$1())) // using parser Int
40+
* val args2 = mainAnnot.varargGetter[Int](info.parameters(2), mainArgs.drop(2)) // using parser Int
4141
* mainAnnot.run(() => sum(args0(), args1(), args2()*))
4242
* }
4343
* }
@@ -49,6 +49,7 @@ package scala.annotation
4949
*/
5050
@experimental
5151
trait MainAnnotation[Parser[_], Result] extends StaticAnnotation:
52+
import MainAnnotation.{CommandInfo, ParameterInfo}
5253

5354
/** Process the command arguments before parsing them.
5455
*
@@ -61,17 +62,17 @@ trait MainAnnotation[Parser[_], Result] extends StaticAnnotation:
6162
* @param info The information about the command (name, documentation and info about parameters)
6263
* @param args The command line arguments
6364
*/
64-
def command(info: MainAnnotation.CommandInfo, args: Seq[String]): Option[Seq[String]]
65+
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]]
6566

6667
/** The getter for the `idx`th argument of type `T`
6768
*
6869
* @param idx The index of the argument
6970
* @param defaultArgument Optional lambda to instantiate the default argument
7071
*/
71-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using Parser[T]): () => T
72+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using Parser[T]): () => T
7273

7374
/** The getter for a final varargs argument of type `T*` */
74-
def varargGetter[T](args: Seq[String])(using Parser[T]): () => Seq[T]
75+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using Parser[T]): () => Seq[T]
7576

7677
/** Run `program` if all arguments are valid if all arguments are valid
7778
*
@@ -110,7 +111,7 @@ object MainAnnotation:
110111
* @param documentation The documentation of the parameter (from `@param` documentation in the main method)
111112
* @param annotations The annotations of the parameter that extend `ParameterAnnotation`
112113
*/
113-
final class ParameterInfo (
114+
final class ParameterInfo(
114115
val name: String,
115116
val typeName: String,
116117
val hasDefault: Boolean,

tests/run/main-annotation-example.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class myMain extends MainAnnotation[FromString, Int]:
4848
else
4949
Some(args)
5050

51-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
51+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using parser: FromString[T]): () => T =
5252
() => parser.fromString(arg)
5353

54-
def varargGetter[T](args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
54+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using parser: FromString[T]): () => Seq[T] =
5555
() => args.map(arg => parser.fromString(arg))
5656

5757
def run(program: () => Int): Unit =

tests/run/main-annotation-homemade-annot-1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class mainAwait(timeout: Int = 2) extends MainAnnotation[FromString, Future[Any]
3535
// This is a toy example, it only works with positional args
3636
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
3737

38-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
38+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
3939
() => p.fromString(arg)
4040

41-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] =
41+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
4242
() => for arg <- args yield p.fromString(arg)
4343

4444
def run(f: () => Future[Any]): Unit = println(Await.result(f(), Duration(timeout, SECONDS)))

tests/run/main-annotation-homemade-annot-2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class myMain(runs: Int = 3)(after: String*) extends MainAnnotation[FromString, A
3434

3535
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
3636

37-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
37+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T =
3838
() => p.fromString(arg)
3939

40-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] =
40+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] =
4141
() => for arg <- args yield p.fromString(arg)
4242

4343
def run(f: () => Any): Unit =

tests/run/main-annotation-homemade-annot-3.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class mainNoArgs extends MainAnnotation[FromString, Any]:
1616

1717
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
1818

19-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
19+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
2020

21-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
21+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
2222

2323
def run(program: () => Any): Unit = program()

tests/run/main-annotation-homemade-annot-4.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class mainManyArgs(i1: Int, s2: String, i3: Int) extends MainAnnotation[FromStri
1616

1717
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
1818

19-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
19+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
2020

21-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
21+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
2222

2323

2424
def run(program: () => Any): Unit = program()

tests/run/main-annotation-homemade-annot-5.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class mainManyArgs(o: Option[Int]) extends MainAnnotation[FromString, Any]:
1818

1919
def command(info: CommandInfo, args: Seq[String]): Option[Seq[String]] = Some(args)
2020

21-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
21+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = ???
2222

23-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
23+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = ???
2424

2525
def run(program: () => Any): Unit = program()

tests/run/main-annotation-homemade-annot-6.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class myMain extends MainAnnotation[Make, Any]:
3333
|)""".stripMargin)
3434
Some(args)
3535

36-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: Make[T]): () => T =
36+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: Make[T]): () => T =
3737
() => p.make
3838

39-
def varargGetter[T](args: Seq[String])(using p: Make[T]): () => Seq[T] =
39+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: Make[T]): () => Seq[T] =
4040
println("varargGetter()")
4141
() => Seq(p.make, p.make)
4242

tests/run/main-annotation-newMain.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,15 @@ final class newMain extends MainAnnotation[FromString, Any]:
292292
case Some(t) => () => t
293293
case None => error(s"invalid argument for $argName: $arg")
294294

295-
def argGetter[T](idx: Int, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = {
296-
val name = info.parameters(idx).name
297-
if arg.nonEmpty then convert(name, arg, p)
295+
def argGetter[T](param: ParameterInfo, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = {
296+
if arg.nonEmpty then convert(param.name, arg, p)
298297
else defaultArgument match
299298
case Some(defaultGetter) => defaultGetter
300-
case None => error(s"missing argument for $name")
299+
case None => error(s"missing argument for ${param.name}")
301300
}
302301

303-
def varargGetter[T](args: Seq[String])(using p: FromString[T]): () => Seq[T] = {
304-
val name = info.parameters.last.name
305-
val getters = args.map(arg => convert(name, arg, p))
302+
def varargGetter[T](param: ParameterInfo, args: Seq[String])(using p: FromString[T]): () => Seq[T] = {
303+
val getters = args.map(arg => convert(param.name, arg, p))
306304
() => getters.map(_())
307305
}
308306

0 commit comments

Comments
 (0)