问题描述
我正在使用Apache commons cli(1.2)进行命令行解析.
I am using Apache commons cli (1.2) for command line parsing.
我的代码中包含以下内容:
I have the following in my code:
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()
,则没有区别.
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder
. It makes no difference if I change .hasArg
to .hasArg()
.
为什么?
顺便说一句,Java很好地解决了这个问题.
BTW, Java parses this fine.
推荐答案
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()
,则没有区别.
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder
. It makes no difference if I change .hasArg
to .hasArg()
.
为什么?
因为 OptionBuilder
中没有实例方法hasArg
,所以只有静态方法.由于hasArg
是静态方法,显然您需要在类上调用它,而不是在类的实例上调用它.
Because there is no instance method hasArg
in OptionBuilder
, only a static method. Since hasArg
is a static method, you obviously need to call it on the class, not on an instance of the class.
我不知道这与解析有什么关系. Scala对此也进行了解析.另外,某些完全不同的编程对该代码执行或不执行的操作完全无关紧要,因为这是Scala代码,而不是其他某种语言.
I don't understand what this has to do with parsing. Scala parses this just fine, too. Plus, what some completely different programming does or doesn't do with this code is utterly irrelevant, since this is Scala code, not some other language.
您需要执行以下操作:
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host")
OptionBuilder.hasArg
OptionBuilder.withDescription("Name of the database host")
val optionParser = OptionBuilder.create('h')
这篇关于Scala错误编译OptionBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!