Last active
August 29, 2015 14:10
-
-
Save shajra/073a0398a76ddea505c7 to your computer and use it in GitHub Desktop.
illustration of my usage of scala-optparse-applicative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ java -jar ./target/scala-2.11/actionstat-builtwith-load-assembly-0.0.154-g3030fdc-dirty.jar --help | |
Invalid option `--help' | |
Usage: builtwith-load <FILE> (-a|--address <ADDRESS>) (-k|--keyspace <NAME>) (-t|--table <NAME>) | |
(-T|--timestamp <NAME>) | |
Loads hosting provider data from a zip file generated by a Builtwith Firehose process | |
to Cassandra. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package actionstat.builtwith.load | |
package config | |
import java.io.File | |
import java.lang.String | |
import scala.{ StringContext, Unit } | |
import scala.collection.immutable.List | |
import net.bmjames.opts | |
import net.bmjames.opts.InfoMod | |
import net.bmjames.opts.builder._ | |
import net.bmjames.opts.types.{ Parser, ReadM } | |
import scalaz.{ Foldable, NonEmptyList } | |
import scalaz.concurrent.Task | |
import scalaz.syntax.apply._ | |
import scalaz.syntax.foldable._ | |
object ParseOpts { | |
def run[F[_] : Foldable] | |
(job: Config => Task[Unit], args: F[String]) | |
: Task[Unit] = { | |
val argArray = args.toList.toArray | |
Task | |
.delay { opts execParser (argArray, progName, parserInfo) } | |
.flatMap(job) | |
} | |
private def progName: String = "builtwith-load" | |
private def parserInfo = | |
opts info (parser, progDesc) | |
private def progDesc: InfoMod[Config] = | |
opts progDesc ( | |
"""Loads hosting provider data from a zip file generated by a """ + | |
"""Builtwith Firehose process.""") | |
// a small hack to deal with a bug | |
// | |
import net.bmjames.opts.types.ParserM.{ fromM, oneM, manyM } | |
def some[A](p: Parser[A]): Parser[NonEmptyList[A]] = | |
fromM((oneM(p) |@| manyM(p))(NonEmptyList.nel)) | |
private def parser: Parser[Config] = { | |
val readFilenames: Parser[NonEmptyList[BuiltwithOutput]] = | |
some( | |
strArgument( | |
help("zip file from Builtwith Firehose job"), | |
metavar("<FILE>")) map { f => BuiltwithOutput(new File(f)) }) | |
val contactPoints: Parser[NonEmptyList[ContactPoint]] = | |
some( | |
strOption( | |
short('a'), | |
long("address"), | |
help("IP or hostname of Cassandra server"), | |
metavar("<ADDRESS>")) map ContactPoint) | |
val keyspace: Parser[Keyspace] = | |
strOption( | |
short('k'), | |
long("keyspace"), | |
help("keyspace of table to load"), | |
metavar("<NAME>")) map Keyspace | |
val table: Parser[Table] = | |
strOption( | |
short('t'), | |
long("table"), | |
help("table to load"), | |
metavar("<NAME>")) map Table | |
val timestamp: Parser[FactTimestamp] = { | |
val malformedMsg = "timestamp is not well-formed (ISO 8601 accepted)" | |
option( | |
eitherReader(FactTimestamp parse malformedMsg), | |
short('T'), | |
long("timestamp"), | |
help("table to load"), | |
metavar("<NAME>")) | |
} | |
(readFilenames |@| | |
contactPoints |@| | |
keyspace |@| | |
table |@| | |
timestamp)(Config.apply) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment