我想使用 args4j我的 Scala v2.10 应用程序中的 Java 库。但是,我遇到了一些麻烦。例如,我有一个类来存储 Java 中的参数,如下所示。
public class MyArgs {
@Option(name = "-i", usage = "file input", required = true)
private String input;
public String getInput() { return input; }
}
然后我有一个如下的 Scala 程序。
object TestArgs {
def main(args: Array[String]): Unit = {
val myArgs = new MyArgs()
val cmdLineParser = new CmdLineParser()
try {
cmdLineParser.parseArgument(java.util.Arrays.asList(args: _*))
} catch {
case e: Exception => {
System.err.println(e.getMessage)
}
}
}
}
在 IntelliJ 中,我传递了类似 -i some/path/to/file.txt
的内容,但我不断收到以下消息。
"-i" is not a valid option
对正在发生的事情有什么想法吗?
最佳答案
可以直接使用args4j Java库;但是,我可以推荐这个包装库吗?
这个实现适合我使用 Scala 2.10、Spark 1.6
// import the Java args4j
import org.kohsuke.args4j.Option
// import the scala wrapper
import OptionsHelper.optionsOrExit
object SparkScalaTest {
def main(args: scala.Array[String]) {
// setupt the options
class MyOptions extends OptionsWithHelp {
@Option(name="--foo", aliases=Array("-f"), usage="foo bar", required=false)
var update: Boolean = false // this is the default value, since it is not required
}
//Parse arguments
val options = optionsOrExit(args, new MyOptions)
sc.stop
}
}
关于java - Java库Args4j和Scala 2.10之间的互操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623774/