Java的Apache的CLI OptionBuilder没有发挥Builder模式 [英] Java Apache CLI OptionBuilder not working as Builder pattern

查看:1005
本文介绍了Java的Apache的CLI OptionBuilder没有发挥Builder模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做这样的事情。

public static final Option job1 =
    OptionBuilder.hasArg(false)
        .isRequired(false)
        .withDescription("description of job1")
        .create(JOB1);

如上所述<一href=\"http://stackoverflow.com/questions/9304431/how-to-specify-multiple-options-using-apache-commons-cli\">How指定使用Apache CLI多个选项?

我使用maven依赖性为

I am using maven dependency as

<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.1</version>
</dependency>

这里提到 - <一个href=\"http://mvnrepository.com/artifact/commons-cli/commons-cli/1.1\">http://mvnrepository.com/artifact/commons-cli/commons-cli/1.1

但我不能,编译器抱怨

静态成员org.apache.commons.cli.OptionsBuilder.create()通过实例引用访问

,我甚至试过&LT;版本&GT; 1.2&LT; /版本&GT; ,但没有运气,我失去的东西。

, I even tried with <version>1.2</version>, but no luck, am I missing something?

推荐答案

现在的问题是,在每一个方法 OptionBuilder 是静态的,在静态字段操作,并返回一个静态实例。因此,你不需要 OptionBuilder 的一个实例来执行的方法。这并不与自然的欲望链嫁得好呼叫在一起,因为你已经做到了。

The problem is that every method in OptionBuilder is static, operating on static fields and returning a single static instance. Hence you don't require an instance of OptionBuilder to execute the methods. This doesn't marry well with the natural desire to chain the calls together, as you've done.

有比其他无解或者平静下来的编译器(可能是禁用的警告在你的IDE?),或者调整code如下(未经测试):

There is no solution other than to either calm the compiler down (perhaps disabling warnings in your IDE?) or adjust your code as follows (untested):

public static final Option job1;

static {
    OptionBuilder.hasArg(false);
    OptionBuilder.isRequired(false)
    OptionBuilder.withDescription("description of job1")
    job1 = OptionBuilder.create(JOB1);
}

如果在 OptionBuilder 类是使用一个公共的无参数的构造函数,只有实例方法重写,从而表现像其他制造商那里会更好。有一个在公共-CLI问题跟踪强调这一现有错误:<一href=\"https://issues.apache.org/jira/browse/CLI-224\">https://issues.apache.org/jira/browse/CLI-224

It would be better if the OptionBuilder class was rewritten with a public no-argument constructor and only instance methods, thus behaving like every other builder out there. There is an existing bug in the commons-cli issue tracker highlighting this: https://issues.apache.org/jira/browse/CLI-224

更新:我的补丁已经提交到主干,所以一个新的正确的建设者将在公地CLI(V1.3)的下一个版本中提供。请参阅Javadocs <一个href=\"http://commons.apache.org/proper/commons-cli/apidocs/org/apache/commons/cli/Option.Builder.html\">here.

这篇关于Java的Apache的CLI OptionBuilder没有发挥Builder模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆