Created
August 17, 2012 00:43
-
-
Save masahitojp/3374826 to your computer and use it in GitHub Desktop.
args4J をScalaから使う
This file contains hidden or 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 com.github.masahitojp | |
import org.kohsuke.args4j.CmdLineException | |
import org.kohsuke.args4j.CmdLineParser | |
import org.kohsuke.args4j.Option | |
import grizzled.slf4j.Logger | |
import scala.collection.JavaConversions._ | |
import scala.util.control.Exception._ | |
object Args4j { | |
val logger = Logger[this.type] | |
def main(args: Array[String]) { | |
val args1 = args.toList | |
val args4j: Args4j = new Args4j | |
val parser= new CmdLineParser(args4j) | |
allCatch either parser.parseArgument(args1) match { | |
case Right(x) => { | |
if (!args4j.doPrintUsage){ | |
System.out.println("ID:" + args4j.id) | |
System.out.println("Name:" + args4j.name) | |
System.out.println("Debug:" + args4j.isDebug) | |
System.out.println("Optional:" + args4j.optional) | |
} | |
else{ | |
parser.printUsage(System.out) | |
} | |
} | |
case Left(e:CmdLineException) => { | |
logger.warn(e.printStackTrace()) | |
parser.printSingleLineUsage(System.out) | |
} | |
} | |
} | |
} | |
class Args4j { | |
@Option(name = "-i", metaVar = "id", usage = "user id") private var id: Int = 0 | |
@Option(name = "-n", metaVar = "name", usage = "user name") private var name: String = null | |
@Option(name = "-h", aliases = Array("--help"), usage = "print help") private var doPrintUsage: Boolean = false | |
@Option(name = "-d", aliases = Array("--debug"), usage = "debug option") private var isDebug: Boolean = false | |
@Option(name = "-o", usage = "optional args", required = false) private var optional: String = null | |
} |
This file contains hidden or 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
import sbt._ | |
import sbt.Keys._ | |
object Args4sBuild extends Build { | |
lazy val args4s = Project( | |
id = "args4s", | |
base = file("."), | |
settings = Project.defaultSettings ++ Seq( | |
name := "args4s", | |
organization := "com.github.masahitojp", | |
version := "0.1-SNAPSHOT", | |
scalaVersion := "2.9.2" , | |
// add other settings here | |
resolvers += "jenkins-repository" at "http://repo.jenkins-ci.org/public/", | |
libraryDependencies ++= Seq( | |
"args4j" % "args4j" % "2.0.21", | |
// test | |
"org.specs2" %% "specs2" % "1.9" % "test", | |
//log | |
"org.clapper" %% "grizzled-slf4j" % "0.6.9", | |
"ch.qos.logback" % "logback-classic" % "1.0.1" | |
) | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment