Skip to content

Instantly share code, notes, and snippets.

@alexy
Created April 6, 2012 17:53
Show Gist options
  • Save alexy/2321628 to your computer and use it in GitHub Desktop.
Save alexy/2321628 to your computer and use it in GitHub Desktop.
Holder fails, builder parses trailing args allright
import org.rogach.scallop._
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
/**
* Created with IntelliJ IDEA.
* User: Alexy
* Date: 4/6/12
* Time: 10:34 AM
* To change this template use File | Settings | File Templates.
*/
class ScallopTest extends FunSuite with ShouldMatchers {
test ("builder version") {
object b {
val opts = Scallop(List("-k","a","b","c","d"))
.version("1.0 (c) 2012 Klout, Author: Alexy Khrabrov")
// in "verify" stage it would print this message and exit
.banner("""Usage: RankerPairs [OPTION]... <input-file>
|generating HelloKitty pairs
|Options:
|""".stripMargin) // --help is also provided
// will also exit after printing version, banner, and options usage
.opt[Boolean]("key", descr = "read social graph from seq key vs text", default = Some(true))
.opt[String]("date", descr = "only generate pairs for new regs after that date")
.opt[Int]("minRankers", descr = "minimum number of rankers per pair", default = Some(5))
.props('N', "top, middle, bottom numbers of influencers to go into pairs")
.opt[Boolean]("flip", descr = "do we need to append flipped originals", default = Some(false))
.opt[String]("separator", descr = "separator between pair elements", default = Some("tab"))
.trailArg[String]("influencedBy graph")
.trailArg[String]("registered users")
.trailArg[String]("scores file")
.trailArg[String]("output file")
.verify
val fromKey = opts[Boolean]("key")
val date = opts.get[String]("date")
val minRankers = opts[Int]("minRankers")
val nTop: Int = opts.prop('N', "top").getOrElse("30").toInt
val nMiddle: Int = opts.prop('N', "middle").getOrElse("0").toInt
val nBottom: Int = opts.prop('N', "bottom").getOrElse("0").toInt
val flip = opts[Boolean]("flip")
val separator: String = opts[String]("separator") match {
case "tab" => "\t"
case "comma" => ","
case "^A" => "\001" // 0.toChar
case x => sys.error("wrong separator: " + x)
}
val soiByFile = opts[String]("influencedBy graph")
val registeredFile = opts[String]("registered users")
val scoresFile = opts[String]("scores file")
val outputFile = opts[String]("output file")
override def toString =
"\n Reading social graph from " + soiByFile +
"\n registered users from " + registeredFile +
(date match {
case Some(d) => "\n ONLY registered after " + d
case _ => ""
}) +
"\n scores from " + scoresFile +
"\n writing pair-rankers to " + outputFile
def summary: String = opts.summary
}
b.fromKey should equal (true)
b.soiByFile should equal("a")
}
test ("holder version") {
object c extends ScallopConf(List("-k","a","b","c","d")) {
val fromKey = opt[Boolean]("key", descr = "read social graph from seq key vs text", default = Some(true))
val date = opt[String]("date", descr = "only generate pairs for new regs after that date", required = false).get
val minRankers = opt[Int]("minRankers", descr = "minimum number of rankers per pair", default = Some(5))
val pairN = props('N')
val nTop: Int = pairN("top").getOrElse("30").toInt
val nMiddle: Int = pairN("middle").getOrElse("0").toInt
val nBottom: Int = pairN("bottom").getOrElse("0").toInt
val flip = opt[Boolean]("flip", descr = "do we need to append flipped originals", default = Some(false))
val separator: String = opt[String]("separator").get match {
case Some("tab") => "\t"
case Some("comma") => ","
case Some("^A") => "\001" // 0.toChar
case Some(x) => sys.error("wrong separator: " + x)
case None => "\t"
}
val soiByFile = trailArg[String](required = true) // influencedBy graph
val registeredFile = trailArg[String](required = true) // registered users
val scoresFile = trailArg[String](required = true) // scores file
val outputFile = trailArg[String](required = true) // output file
verify
override def toString =
"\n Reading social graph from " + soiByFile() +
"\n registered users from " + registeredFile() +
(date match {
case Some(d) => "\n ONLY registered after " + d
case _ => ""
}) +
"\n scores from " + scoresFile() +
"\n writing pair-rankers to " + outputFile()
}
c.fromKey should equal (true)
c.soiByFile should equal("a")
}
}
@Rogach
Copy link

Rogach commented Apr 7, 2012

Forgive me for the delay, lambdabot got the chance to notify me only right now :) I responded on your issue in scallop.

@alexy
Copy link
Author

alexy commented Apr 8, 2012

Thanks, it's clear now! Makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment