Skip to content

Commit 80ef899

Browse files
Add BigDecimal TokensReader (#152)
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]>
1 parent 9d23b9c commit 80ef899

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

mainargs/src/TokensReader.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ object TokensReader {
131131
def shortName = "double"
132132
def read(strs: Seq[String]) = tryEither(strs.last.toDouble)
133133
}
134+
implicit object BigDecimalRead extends Simple[BigDecimal] {
135+
def shortName = "bigdecimal"
136+
def read(strs: Seq[String]) = tryEither(BigDecimal(strs.last))
137+
}
134138

135139
implicit def LeftoverRead[T: TokensReader.Simple]: TokensReader.Leftover[mainargs.Leftover[T], T] =
136140
new LeftoverRead[T]()(implicitly[TokensReader.Simple[T]])

mainargs/test/src/ManyTests.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ object ManyTests extends TestSuite {
1515
i: Boolean,
1616
j: String,
1717
k: Int,
18-
l: Boolean
18+
l: Boolean,
19+
m: Double,
20+
n: BigDecimal
1921
)
2022

2123
val parser = ParserForClass[Config]
@@ -46,7 +48,11 @@ object ManyTests extends TestSuite {
4648
"--k",
4749
"4",
4850
"--l",
49-
"false"
51+
"false",
52+
"--m",
53+
"5.50",
54+
"--n",
55+
"12345678901234567890.12345678901234567890"
5056
),
5157
allowPositional = true
5258
)
@@ -73,7 +79,11 @@ object ManyTests extends TestSuite {
7379
"--k",
7480
"4",
7581
"--l",
76-
"false"
82+
"false",
83+
"--m",
84+
"5.50",
85+
"--n",
86+
"12345678901234567890.12345678901234567890"
7787
),
7888
allowPositional = true
7989
)
@@ -115,7 +125,9 @@ object ManyTests extends TestSuite {
115125
"true",
116126
"J",
117127
"4",
118-
"false"
128+
"false",
129+
"5.50",
130+
"12345678901234567890.12345678901234567890"
119131
),
120132
allowPositional = true
121133
)

0 commit comments

Comments
 (0)