Skip to content

Commit

Permalink
Add BigDecimal TokensReader (#152)
Browse files Browse the repository at this point in the history
I was playing around with `mainargs` for a script of mine and I noticed
that there were no implicit for the `BigDecimal` type. Since I think
could be a quite standard type to use I thought about opening this PR
also thanks to @lolgab

Pull Request: #152

---------

Co-authored-by: Lorenzo Gabriele <[email protected]>
  • Loading branch information
KristianLentino99 and lolgab authored Sep 7, 2024
1 parent 9d23b9c commit 80ef899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions mainargs/src/TokensReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ object TokensReader {
def shortName = "double"
def read(strs: Seq[String]) = tryEither(strs.last.toDouble)
}
implicit object BigDecimalRead extends Simple[BigDecimal] {
def shortName = "bigdecimal"
def read(strs: Seq[String]) = tryEither(BigDecimal(strs.last))
}

implicit def LeftoverRead[T: TokensReader.Simple]: TokensReader.Leftover[mainargs.Leftover[T], T] =
new LeftoverRead[T]()(implicitly[TokensReader.Simple[T]])
Expand Down
20 changes: 16 additions & 4 deletions mainargs/test/src/ManyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ object ManyTests extends TestSuite {
i: Boolean,
j: String,
k: Int,
l: Boolean
l: Boolean,
m: Double,
n: BigDecimal
)

val parser = ParserForClass[Config]
Expand Down Expand Up @@ -46,7 +48,11 @@ object ManyTests extends TestSuite {
"--k",
"4",
"--l",
"false"
"false",
"--m",
"5.50",
"--n",
"12345678901234567890.12345678901234567890"
),
allowPositional = true
)
Expand All @@ -73,7 +79,11 @@ object ManyTests extends TestSuite {
"--k",
"4",
"--l",
"false"
"false",
"--m",
"5.50",
"--n",
"12345678901234567890.12345678901234567890"
),
allowPositional = true
)
Expand Down Expand Up @@ -115,7 +125,9 @@ object ManyTests extends TestSuite {
"true",
"J",
"4",
"false"
"false",
"5.50",
"12345678901234567890.12345678901234567890"
),
allowPositional = true
)
Expand Down

0 comments on commit 80ef899

Please sign in to comment.