Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created June 14, 2012 13:56
Show Gist options
  • Save tototoshi/2930458 to your computer and use it in GitHub Desktop.
Save tototoshi/2930458 to your computer and use it in GitHub Desktop.
scopt 2.1.0 お試し
import sbt._
import sbt.Keys._
object EchoBuild extends Build {
lazy val echo = Project(
id = "echo",
base = file("."),
settings = Project.defaultSettings ++ Seq(
name := "echo",
organization := "com.github.tototoshi.echo",
version := "0.1-SNAPSHOT",
scalaVersion := "2.9.1",
libraryDependencies += "com.github.scopt" %% "scopt" % "2.1.0",
resolvers += "sonatype-public" at "https://oss.sonatype.org/content/groups/public"
)
)
}
package com.github.tototoshi.echo
object Echo {
case class Config(newLine: Boolean, times: Int, string: String)
def main(args: Array[String]): Unit = {
val version = "0.1"
val parser = new scopt.immutable.OptionParser[Config]("echo", version) {
def options = Seq(
flag("n", "no-newline", "no newline") { (config: Config) => config.copy(newLine = false) }
, intOpt("x", "times", "repeat x times") { (times: Int, config: Config) => config.copy(times = times) }
, argOpt("<string>", "string(s) to output") { (string: String, config: Config) => config.copy(string = string) }
)
}
parser.parse(args, Config(true, 1, "")).map { config =>
for (i <- 1 to config.times) {
if (config.newLine) {
println(config.string)
} else {
print(config.string)
}
}
} getOrElse {
println("Invalid arguments.")
}
}
}
/**
> run -x 3 hello
[info] Running com.github.tototoshi.echo.Echo -x 3 hello
hello
hello
hello
[success] Total time: 0 s, completed 2012/06/14 22:53:28
> run hello
[info] Running com.github.tototoshi.echo.Echo hello
hello
[success] Total time: 0 s, completed 2012/06/14 22:53:31
> run -x 3 -n hello
[info] Running com.github.tototoshi.echo.Echo -x 3 -n hello
hellohellohello[success] Total time: 0 s, completed 2012/06/14 22:53:36
> run -x 3 --no-newline hello
[info] Running com.github.tototoshi.echo.Echo -x 3 --no-newline hello
hellohellohello[success] Total time: 0 s, completed 2012/06/14 22:53:44
> run -a
[info] Running com.github.tototoshi.echo.Echo -a
Error: Unknown argument '-a'
echo 0.1
Usage: echo [options] <string>
-n | --no-newline
no newline
-x <value> | --times <value>
repeat x times
<string>
string(s) to output
Invalid arguments.
[success] Total time: 0 s, completed 2012/06/14 22:54:08
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment