From 7ce0b26d4f2be01f10c8f54c80a347095fcb1fb9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 6 May 2019 11:33:17 +0100 Subject: [PATCH] [parseArgs] refactoring --- .../scala/net/kemitix/s3thorp/ParseArgs.scala | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala b/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala index dac5509..9654563 100644 --- a/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala +++ b/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala @@ -1,32 +1,35 @@ package net.kemitix.s3thorp +import scopt.OParser +import scopt.OParser.{builder,sequence, parse} 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 { + val defaultConfig = Config("def-bucket", "def-prefix", "def-source") + + val configParser: OParser[Unit, Config] = { + val parserBuilder = builder[Config] + import parserBuilder._ + sequence( + programName("S3Thorp"), + head("s3thorp", "0.1.0"), + opt[String]('s', "source") + .action((str, c) => c.copy(source = str)) + .text("Source directory to sync to S3"), + 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") + ) + } + + def apply(args: List[String]): IO[Config] = + parse(configParser, args, defaultConfig) match { case Some(config) => IO.pure(config) case _ => IO.raiseError(new IllegalArgumentException) } - } }