From 1a7aebb5cf7e087e2db9fa4b132c35434ded099d Mon Sep 17 00:00:00 2001
From: Gleb Koval <gleb@koval.net>
Date: Fri, 31 Jan 2025 16:29:30 +0000
Subject: [PATCH] feat: use cli parsing library and return randomly 0, 100 or
 200

---
 project.scala            |  1 +
 src/main/wacc/Main.scala | 42 +++++++++++++++++++++++++++++-----------
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/project.scala b/project.scala
index 7cf3277..7a42a9c 100644
--- a/project.scala
+++ b/project.scala
@@ -4,6 +4,7 @@
 // dependencies
 //> using dep com.github.j-mie6::parsley::5.0.0-M10
 //> using dep com.lihaoyi::os-lib::0.11.3
+//> using dep com.github.scopt::scopt::4.1.0
 //> using test.dep org.scalatest::scalatest::3.2.19
 
 // these are all sensible defaults to catch annoying issues
diff --git a/src/main/wacc/Main.scala b/src/main/wacc/Main.scala
index a4c9fd7..1aa3eb3 100644
--- a/src/main/wacc/Main.scala
+++ b/src/main/wacc/Main.scala
@@ -1,16 +1,36 @@
 package wacc
 
-import parsley.{Success, Failure}
+import scopt.OParser
+import java.io.File
 
-def main(args: Array[String]): Unit = {
-  println("hello WACC!")
+case class CliConfig(
+    file: File = new File(".")
+)
 
-  args.headOption match {
-    case Some(expr) =>
-      parser.parse(expr) match {
-        case Success(x)   => println(s"$expr = $x")
-        case Failure(msg) => println(msg)
-      }
-    case None => println("please enter an expression")
-  }
+val cliBuilder = OParser.builder[CliConfig]
+val cliParser = {
+  import cliBuilder._
+  OParser.sequence(
+    programName("wacc-compiler"),
+    help('h', "help")
+      .text("Prints this help message"),
+    arg[File]("<file>")
+      .text("Input WACC source file")
+      .required()
+      .action((f, c) => c.copy(file = f))
+      .validate(f =>
+        if (!f.exists) failure("File does not exist")
+        else if (!f.isFile) failure("File must be a regular file")
+        else if (!f.getName.endsWith(".wacc"))
+          failure("File must have .wacc extension")
+        else success
+      )
+  )
 }
+
+def main(args: Array[String]): Unit =
+  OParser.parse(cliParser, args, CliConfig()) match {
+    case Some(config) =>
+      System.exit(scala.util.Random.between(0, 3) * 100)
+    case None =>
+  }