[parseArgs] refactoring

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

View file

@ -1,32 +1,35 @@
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] = { val parserBuilder = builder[Config]
import builder._ import parserBuilder._
OParser.sequence( 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))
opt[String]('b', "bucket") .text("Source directory to sync to S3"),
.action((str, c) => c.copy(bucket = str)) opt[String]('b', "bucket")
.text("S3 bucket name"), .action((str, c) => c.copy(bucket = str))
opt[String]('p', "prefix") .text("S3 bucket name"),
.action((str, c) => c.copy(prefix = str)) opt[String]('p', "prefix")
.text("Prefix within the S3 Bucket") .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 {
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)
} }
}
} }