2011-02-04 92 views
5

我使用Apache commons cli(1.2)进行命令行解析。Scala错误编译OptionBuilder

我在我的代码如下:

import org.apache.commons.cli.OptionBuilder 
OptionBuilder.withLongOpt("db-host").hasArg. 
withDescription("Name of the database host").create('h') 

我得到的错误hasArg is not a member of org.apache.commons.cli.OptionBuilder。如果我将.hasArg更改为.hasArg(),则没有区别。

为什么?

顺便说一句,Java解析这个罚款。

+0

(提醒:打开在javac的/ Eclipse中/何警告。) – 2011-02-04 22:21:06

+0

@pst:我有他们。我在Netbeans(最好的Scala支持,恕我直言)工作,它强调`hasArg`方法。我更喜欢在IntelliJ中工作,但Scala插件在重新格式化代码中存在一些严重的错误。我提交了错误报告,但到目前为止,没有发布修复程序。 – Ralph 2011-02-05 13:53:05

回答

12
import org.apache.commons.cli.OptionBuilder 
OptionBuilder.withLongOpt("db-host").hasArg. 
withDescription("Name of the database host").create('h') 

我得到的错误hasArg is not a member of org.apache.commons.cli.OptionBuilder。如果我将.hasArg更改为.hasArg(),则没有区别。

为什么?

因为没有实例方法hasArgOptionBuilder,只是一个静态方法。由于hasArg是一种静态方法,您显然需要在类上调用它,而不是在类的实例上调用它。

顺便说一句,Java解析这个罚款。

我不明白这与解析有什么关系。 Scala也解析了这一点。另外,一些完全不同的编程与此代码做或不做的是完全不相关的,因为这是Scala代码,而不是其他语言。

你需要做这样的事情:

import org.apache.commons.cli.OptionBuilder 

OptionBuilder.withLongOpt("db-host") 
OptionBuilder.hasArg 
OptionBuilder.withDescription("Name of the database host") 

val optionParser = OptionBuilder.create('h')