[parseArgs] extract to object

This commit is contained in:
Paul Campbell 2019-05-06 11:28:44 +01:00
parent a4cf1d716b
commit d73f42e9ea
2 changed files with 33 additions and 26 deletions

View file

@ -5,31 +5,6 @@ import cats.effect.{ExitCode, IO, IOApp}
object Main extends IOApp { object Main extends IOApp {
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]('s', "source")
.action((str, c) => c.copy(source = str)),
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", "def-source")
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 putStrLn(value: String) = IO { println(value) }
def sync(c: Config): IO[Unit] = def sync(c: Config): IO[Unit] =
@ -40,7 +15,7 @@ object Main extends IOApp {
def program(args: List[String]): IO[ExitCode] = def program(args: List[String]): IO[ExitCode] =
for { for {
a <- parseArgs(args) a <- ParseArgs(args)
_ <- sync(a) _ <- sync(a)
} yield ExitCode.Success } yield ExitCode.Success

View file

@ -0,0 +1,32 @@
package net.kemitix.s3thorp
import cats.effect.IO
object ParseArgs {
def apply(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]('s', "source")
.action((str, c) => c.copy(source = str)),
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", "def-source")
OParser.parse(configParser, args, defaultConfig) match {
case Some(config) => IO.pure(config)
case _ => IO.raiseError(new IllegalArgumentException)
}
}
}