Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow special characters in main method names and argument names #13

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions mainargs/src/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Macros(val c: Context) {
val companionObj = weakTypeOf[T].typeSymbol.companion
val constructor = cls.primaryConstructor.asMethod
val route = extractMethod(
"apply",
TermName("apply"),
constructor.paramLists.flatten,
constructor.pos,
cls.annotations.find(_.tpe =:= typeOf[main]),
Expand Down Expand Up @@ -76,7 +76,7 @@ class Macros(val c: Context) {
(vararg, unwrappedType)
}

def extractMethod(methodName: String,
def extractMethod(methodName: TermName,
flattenedArgLists: Seq[Symbol],
methodPos: Position,
mainAnnotation: Option[Annotation],
Expand Down Expand Up @@ -114,12 +114,12 @@ class Macros(val c: Context) {
}
val argSig = if (vararg) q"""
_root_.mainargs.ArgSig.createVararg[$varargUnwrappedType, $curCls](
${arg.name.toString},
${arg.name.decoded},
$instantiateArg,
).widen[_root_.scala.Any]
""" else q"""
_root_.mainargs.ArgSig.create[$varargUnwrappedType, $curCls](
${arg.name.toString},
${arg.name.decoded},
$instantiateArg,
$defaultOpt
).widen[_root_.scala.Any]
Expand All @@ -144,11 +144,11 @@ class Macros(val c: Context) {

val res = q"""{
_root_.mainargs.MainData.create[$returnType, $curCls](
$methodName,
${methodName.decoded},
$mainInstance,
_root_.scala.Seq(..$argSigs),
($baseArgSym: $curCls, $argListSymbol: _root_.scala.Seq[_root_.scala.Any]) => {
$baseArgSym.${TermName(methodName)}(..$argNameCasts)
$baseArgSym.$methodName(..$argNameCasts)
}
)
}"""
Expand All @@ -163,7 +163,7 @@ class Macros(val c: Context) {
for(t <- getValsOrMeths(curCls) if pred(t))
yield {
extractMethod(
t.name.toString,
t.name,
t.paramss.flatten,
t.pos,
t.annotations.find(_.tpe =:= typeOf[main]),
Expand Down
29 changes: 29 additions & 0 deletions mainargs/test/src/DashedArgumentName.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mainargs
import utest._


object DashedArgumentName extends TestSuite{

object Base{
@main
def `opt-for-18+name`(`opt-for-18+arg`: Boolean) = `opt-for-18+arg`
@main
def `opt-for-29+name`(`opt-for-29+arg`: Boolean) = `opt-for-29+arg`
}
val check = new Checker(ParserForMethods(Base), allowPositional = true)

val tests = Tests {
test - check(
List("opt-for-18+name", "--opt-for-18+arg", "true"), Result.Success(true)
)
test - check(
List("opt-for-18+name", "--opt-for-18+arg", "false"), Result.Success(false)
)
test - check(
List("opt-for-29+name", "--opt-for-29+arg", "true"), Result.Success(true)
)
test - check(
List("opt-for-29+name", "--opt-for-29+arg", "false"), Result.Success(false)
)
}
}