Skip to content

Commit c040077

Browse files
committed
Simplify args kind logic using ParamInfo
1 parent f6304a8 commit c040077

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

tests/run/main-annotation-newMain.scala

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ final class newMain extends MainAnnotation:
3838

3939
override def command(args: Array[String], commandName: String, documentation: String, parameterInfos: ParameterInfo*) =
4040
new Command[Parser, Result]:
41-
private enum ArgumentKind {
42-
case SimpleArgument, OptionalArgument, VarArgument
43-
}
4441

4542
private val argMarker = "--"
4643
private val shortArgMarker = "-"
@@ -114,9 +111,6 @@ final class newMain extends MainAnnotation:
114111
(pa, nameToArgValues, ia)
115112
}
116113

117-
/** The kind of the arguments. Used to display help about the main method. */
118-
private val argKinds = new mutable.ArrayBuffer[ArgumentKind]
119-
120114
/** A buffer for all errors */
121115
private val errors = new mutable.ArrayBuffer[String]
122116

@@ -147,19 +141,15 @@ final class newMain extends MainAnnotation:
147141

148142
private def usage(): Unit =
149143
def argsUsage: Seq[String] =
150-
for ((infos, kind) <- parameterInfos.zip(argKinds))
151-
yield {
152-
val canonicalName = getNameWithMarker(infos.name)
153-
val shortNames = getShortNames(infos).map(getNameWithMarker)
154-
val alternativeNames = getAlternativeNames(infos).map(getNameWithMarker)
144+
for info <- parameterInfos yield
145+
val canonicalName = getNameWithMarker(info.name)
146+
val shortNames = getShortNames(info).map(getNameWithMarker)
147+
val alternativeNames = getAlternativeNames(info).map(getNameWithMarker)
155148
val namesPrint = (canonicalName +: alternativeNames ++: shortNames).mkString("[", " | ", "]")
156-
157-
kind match {
158-
case ArgumentKind.SimpleArgument => s"$namesPrint <${infos.typeName}>"
159-
case ArgumentKind.OptionalArgument => s"[$namesPrint <${infos.typeName}>]"
160-
case ArgumentKind.VarArgument => s"[<${infos.typeName}> [<${infos.typeName}> [...]]]"
161-
}
162-
}
149+
if info.isVarargs then s"[<${info.typeName}> [<${info.typeName}> [...]]]"
150+
else if info.hasDefault then s"[$namesPrint <${info.typeName}>]"
151+
else s"$namesPrint <${info.typeName}>"
152+
end for
163153

164154
def wrapArgumentUsages(argsUsage: Seq[String], maxLength: Int): Seq[String] = {
165155
def recurse(args: Seq[String], currentLine: String, acc: Vector[String]): Seq[String] =
@@ -204,24 +194,21 @@ final class newMain extends MainAnnotation:
204194
val argDocShift = argNameShift + 2
205195

206196
println("Arguments:")
207-
for ((infos, kind) <- parameterInfos.zip(argKinds))
208-
val canonicalName = getNameWithMarker(infos.name)
209-
val shortNames = getShortNames(infos).map(getNameWithMarker)
210-
val alternativeNames = getAlternativeNames(infos).map(getNameWithMarker)
197+
for info <- parameterInfos do
198+
val canonicalName = getNameWithMarker(info.name)
199+
val shortNames = getShortNames(info).map(getNameWithMarker)
200+
val alternativeNames = getAlternativeNames(info).map(getNameWithMarker)
211201
val otherNames = (alternativeNames ++: shortNames) match {
212202
case Seq() => ""
213203
case names => names.mkString("(", ", ", ") ")
214204
}
215205
val argDoc = StringBuilder(" " * argNameShift)
216-
argDoc.append(s"$canonicalName $otherNames- ${infos.typeName}")
206+
argDoc.append(s"$canonicalName $otherNames- ${info.typeName}")
217207

218-
kind match {
219-
case ArgumentKind.OptionalArgument => argDoc.append(" (optional)")
220-
case ArgumentKind.VarArgument => argDoc.append(" (vararg)")
221-
case _ =>
222-
}
208+
if info.isVarargs then argDoc.append(" (vararg)")
209+
else if info.hasDefault then argDoc.append(" (optional)")
223210

224-
val doc = infos.documentation
211+
val doc = info.documentation
225212
if (doc.nonEmpty) {
226213
val shiftedDoc =
227214
doc.split("\n").nn
@@ -231,6 +218,7 @@ final class newMain extends MainAnnotation:
231218
}
232219

233220
println(argDoc)
221+
end for
234222
}
235223
end explain
236224

@@ -248,7 +236,6 @@ final class newMain extends MainAnnotation:
248236

249237
override def argGetter[T](idx: Int, optDefaultGetter: Option[() => T])(using p: Parser[T]): () => T =
250238
val name = parameterInfos(idx).name
251-
argKinds += (if optDefaultGetter.nonEmpty then ArgumentKind.OptionalArgument else ArgumentKind.SimpleArgument)
252239
val parameterInfo = nameToParameterInfo(name)
253240

254241
byNameArgs.get(name) match {
@@ -273,7 +260,6 @@ final class newMain extends MainAnnotation:
273260

274261
override def varargGetter[T](using p: Parser[T]): () => Seq[T] =
275262
val name = parameterInfos.last.name
276-
argKinds += ArgumentKind.VarArgument
277263

278264
val byNameGetters = byNameArgs.getOrElse(name, Seq()).map(arg => convert(name, arg, p))
279265
val positionalGetters = positionalArgs.removeAll.map(arg => convert(name, arg, p))

0 commit comments

Comments
 (0)