From 8a18171d7d010773e057f3fb5d294e47404f24fd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 6 May 2019 10:50:41 +0100 Subject: [PATCH] [main] initial parsing of command line args --- build.sbt | 1 + src/main/scala/net/kemitix/s3thorp/Main.scala | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index c12ee27..f9f8877 100644 --- a/build.sbt +++ b/build.sbt @@ -6,6 +6,7 @@ scalaVersion := "2.12.8" libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.7" % "test" libraryDependencies += "org.typelevel" %% "cats-effect" % "1.3.0" withSources() withJavadoc() +libraryDependencies += "com.github.scopt" %% "scopt" % "4.0.0-RC2" scalacOptions ++= Seq( "-feature", diff --git a/src/main/scala/net/kemitix/s3thorp/Main.scala b/src/main/scala/net/kemitix/s3thorp/Main.scala index 9ac86ea..659eb1f 100644 --- a/src/main/scala/net/kemitix/s3thorp/Main.scala +++ b/src/main/scala/net/kemitix/s3thorp/Main.scala @@ -5,13 +5,35 @@ import cats.effect.{ExitCode, IO, IOApp} object Main extends IOApp { - def parseArgs(args: List[String]): IO[Config] = IO.pure(Config("", "")) + def parseArgs(args: List[String]): IO[Config] = { + import scopt.OParser + val builder = OParser.builder[Config] + val configParser: OParser[Unit, Config] = { + import builder._ + OParser.sequence( + programName("S3Thorp"), + head("s3thorp", "0.1.0"), + opt[String]('b', "bucket") + .action((str, c) => c.copy(bucket = str)) + .text("S3 bucket name"), + opt[String]('p', "prefix") + .action((str, c) => c.copy(prefix = str)) + .text("Prefix within the S3 Bucket") + ) + } + val defaultConfig = Config("def-bucket", "def-prefix") + OParser.parse(configParser, args, defaultConfig) match { + case Some(config) => IO.pure(config) + case _ => IO.raiseError(new IllegalArgumentException) + } + } def putStrLn(value: String) = IO { println(value) } def sync(c: Config): IO[Unit] = for { _ <- putStrLn("S3Thorp - hashed sync for s3") + _ <- putStrLn(s"Bucket: ${c.bucket}, Prefix: ${c.prefix}") } yield () def program(args: List[String]): IO[ExitCode] =