From f24aecffa3b643f51927ce4ea00e264bb67c9d0d Mon Sep 17 00:00:00 2001 From: Jonny Date: Mon, 3 Mar 2025 02:10:18 +0000 Subject: [PATCH] fix: remove implicit val causing conflicts with parsing cli arguments --- src/main/wacc/Main.scala | 42 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main/wacc/Main.scala b/src/main/wacc/Main.scala index c85e2db..571103b 100644 --- a/src/main/wacc/Main.scala +++ b/src/main/wacc/Main.scala @@ -18,7 +18,7 @@ import org.typelevel.log4cats.slf4j.Slf4jLogger import org.typelevel.log4cats.Logger import assemblyIR as asm -import java.nio.file.Paths +import cats.data.ValidatedNel /* TODO: @@ -30,34 +30,30 @@ TODO: 6) errors can be handled more gracefully probably */ -given Argument[Path] = Argument.from("path") { str => - val path = - if (str.startsWith("~")) Paths.get(System.getProperty("user.home"), str.drop(1)) // Expand ~ - else Paths.get(str).toAbsolutePath.normalize() // TODO: normalize or not? - - if (path.toString.endsWith(".wacc")) { - ( - Either.cond(Files.exists(path), path, s"File '${path.toAbsolutePath}' does not exist"), - Either.cond( - Files.isRegularFile(path), - path, - s"File '${path.toAbsolutePath}' must be a regular file" - ) - ).mapN((_, _) => path).toValidatedNel - } else { - Right(path).toValidatedNel - } -} - given logger: Logger[IO] = Slf4jLogger.getLogger[IO] val logOpt: Opts[Boolean] = Opts.flag("log", "Enable logging for additional compilation details", short = "l").orFalse -val outputOpt: Opts[Option[Path]] = - Opts.option[Path]("output", "Specify path for output assembly file(s)").orNone +def validateFile(path: Path): ValidatedNel[String, Path] = { + (for { + // TODO: redundant 2nd parameter :( + _ <- Either.cond(Files.exists(path), (), s"File '${path}' does not exist") + _ <- Either.cond(Files.isRegularFile(path), (), s"File '${path}' must be a regular file") + _ <- Either.cond(path.toString.endsWith(".wacc"), (), "File must have .wacc extension") + } yield path).toValidatedNel +} -val filesOpt: Opts[NonEmptyList[Path]] = Opts.arguments[Path]("files") +val filesOpt: Opts[NonEmptyList[Path]] = + Opts.arguments[Path]("files").mapValidated { + _.traverse(validateFile) + } + +// TODO: Is intermediate String necessary +val outputOpt: Opts[Option[Path]] = + Opts + .option[Path]("output", metavar = "path", help = "Output directory for compiled files.") + .orNone def frontend( contents: String