【问题标题】:How to call a java function that has such prototype?如何调用具有此类原型的 java 函数?
【发布时间】:2018-03-14 09:27:15
【问题描述】:

org.apache.commons.cli 的界面如下:

public Option.Builder type(Class<?> type)

设置选项的类型。

参数:

type - the type of the Option

返回:

this builder, to allow method chaining 

如果我想构建一个“int”类型的选项,我应该如何调用这个函数?

【问题讨论】:

  • 我注意到你的问题仍然是“开放的”——因为你没有接受答案。请查看并决定是否要accept 回答。或者让我知道我是否可以做些什么来增强我的输入以使其被接受。接受有助于未来的读者确定问题是否已解决,并对花时间回答你的人表示感谢。谢谢!

标签: java apache-commons-cli


【解决方案1】:

试试这个:

Options options = new Options();
options.addOption(OptionBuilder.withLongOpt("integer-option")
                      .withDescription("description")
                      .withType(Number.class)
                      .hasArg()
                      .withArgName("argname")
                      .create());

【讨论】:

    【解决方案2】:

    看来你是在问如何表达int的Class。

    试试int.class

    【讨论】:

    • 好久没说话了 :) 希望你没事 :)
    • @davidxxx 是的,我也这么认为。在接下来的 4 周里,我将在不同的时区工作,但很高兴看到你还在。但现在我必须去做一些清晨运动。
    • 哦,它是原创的。有趣的事情要处理?我希望有更多的钱! :p 好的形状产生好的设计!
    【解决方案3】:

    您需要将Number.class 用于intInteger

    所以只是给你一个想法:

    Options options = new Options();
    options.addOption(
            OptionBuilder.withDescription("description")
            ...
            .withType(Number.class)
            ...
            .create());
    

    【讨论】:

    • 谢谢。编译通行证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多