[parseArgs] refactoring

This commit is contained in:
Paul Campbell 2019-05-06 11:33:17 +01:00
parent d73f42e9ea
commit 7ce0b26d4f

View file

@ -1,19 +1,22 @@
package net.kemitix.s3thorp package net.kemitix.s3thorp
import scopt.OParser
import scopt.OParser.{builder,sequence, parse}
import cats.effect.IO import cats.effect.IO
object ParseArgs { object ParseArgs {
def apply(args: List[String]): IO[Config] = { val defaultConfig = Config("def-bucket", "def-prefix", "def-source")
import scopt.OParser
val builder = OParser.builder[Config]
val configParser: OParser[Unit, Config] = { val configParser: OParser[Unit, Config] = {
import builder._ val parserBuilder = builder[Config]
OParser.sequence( import parserBuilder._
sequence(
programName("S3Thorp"), programName("S3Thorp"),
head("s3thorp", "0.1.0"), head("s3thorp", "0.1.0"),
opt[String]('s', "source") opt[String]('s', "source")
.action((str, c) => c.copy(source = str)), .action((str, c) => c.copy(source = str))
.text("Source directory to sync to S3"),
opt[String]('b', "bucket") opt[String]('b', "bucket")
.action((str, c) => c.copy(bucket = str)) .action((str, c) => c.copy(bucket = str))
.text("S3 bucket name"), .text("S3 bucket name"),
@ -22,11 +25,11 @@ object ParseArgs {
.text("Prefix within the S3 Bucket") .text("Prefix within the S3 Bucket")
) )
} }
val defaultConfig = Config("def-bucket", "def-prefix", "def-source")
OParser.parse(configParser, args, defaultConfig) match { def apply(args: List[String]): IO[Config] =
parse(configParser, args, defaultConfig) match {
case Some(config) => IO.pure(config) case Some(config) => IO.pure(config)
case _ => IO.raiseError(new IllegalArgumentException) case _ => IO.raiseError(new IllegalArgumentException)
} }
}
} }