Skip to content

Commit ac79eb1

Browse files
Add CaseApp.process helper method (#423)
Allowing to easily parse arguments in a Scala CLI '.sc' file, for example.
1 parent d6084ae commit ac79eb1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

core/shared/src/main/scala/caseapp/core/app/CaseApp.scala

+16
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ abstract class CaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]
173173

174174
object CaseApp {
175175

176+
def process[T](args: Seq[String])(implicit
177+
parser: Parser[T],
178+
help: Help[T]
179+
): (T, RemainingArgs) = {
180+
var values = Option.empty[(T, RemainingArgs)]
181+
val app: CaseApp[T] = new CaseApp[T] {
182+
def run(options: T, args: RemainingArgs): Unit = {
183+
values = Some((options, args))
184+
}
185+
}
186+
app.main(args.toArray)
187+
values.getOrElse {
188+
sys.error("should not happen")
189+
}
190+
}
191+
176192
def parse[T: Parser](args: Seq[String]): Either[Error, (T, Seq[String])] =
177193
Parser[T].parse(args)
178194

0 commit comments

Comments
 (0)