From 4b0a88447be853d9d541d1b7266deb69551e9f1d Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 4 Sep 2024 19:13:37 +0200 Subject: [PATCH] Parse args with scallop --- build.sbt | 1 + .../scala/de/plugh/asciiprooftree/Main.scala | 26 +++++++------------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/build.sbt b/build.sbt index 8f44aa7..d3e4c4d 100644 --- a/build.sbt +++ b/build.sbt @@ -6,4 +6,5 @@ lazy val root = project name := "asciiprooftree", version := "0.0.0", scalaVersion := scala3Version, + libraryDependencies += "org.rogach" %% "scallop" % "5.1.0", ) diff --git a/src/main/scala/de/plugh/asciiprooftree/Main.scala b/src/main/scala/de/plugh/asciiprooftree/Main.scala index 1a5e91f..872e7fb 100644 --- a/src/main/scala/de/plugh/asciiprooftree/Main.scala +++ b/src/main/scala/de/plugh/asciiprooftree/Main.scala @@ -1,24 +1,18 @@ package de.plugh.asciiprooftree import de.plugh.asciiprooftree.file.Formatter +import org.rogach.scallop.* import java.nio.file.{Files, Path} -@main -def main(args: String*): Unit = args match - case Seq() => run() - case Seq(path) => run(Path.of(path)) - case Seq(path, marker) => run(Path.of(path), marker) - case _ => - println("Usage: asciiprooftree [path] [marker]") - System.exit(1) +class Conf(args: Seq[String]) extends ScallopConf(args): + val path: ScallopOption[Path] = trailArg[Path]() + val marker: ScallopOption[String] = opt[String](default = Some("§")) + verify() @main -def testMain(path: String): Unit = run(Path.of(path)) - -def run(path: Path = Path.of(""), marker: String = "§"): Unit = - println(s"Path: $path") - println(s"Marker: $marker") - val text = Files.readString(path) - val newText = Formatter.reformat(text, marker = marker) - Files.writeString(path, newText) +def main(args: String*): Unit = + val conf = new Conf(args) + val text = Files.readString(conf.path()) + val newText = Formatter.reformat(text, marker = conf.marker()) + Files.writeString(conf.path(), newText)