Apache Commons CLI: replacement for deprecated OptionBuilder?

…衆ロ難τιáo~ 提交于 2019-12-10 12:30:53

问题


IntelliJ shows that OptionBuilder is deprecated in this example code from http://commons.apache.org/proper/commons-cli/usage.html.

What should I use as the replacement?

import org.apache.commons.cli.*;

Options options = new Options();
options.addOption(OptionBuilder.withLongOpt( "block-size" )
       .withDescription( "use SIZE-byte blocks" )
       .hasArg()
       .withArgName("SIZE")
       .create());

回答1:


From http://commons.apache.org/proper/commons-cli/javadocs/api-release/index.html

Deprecated. since 1.3, use Option.builder(String) instead

This is the replacement:

Options options = new Options();
Option option = Option.builder("a")
    .longOpt( "block-size" )
    .desc( "use SIZE-byte blocks"  )
    .hasArg()
    .argName( "SIZE" )
    .build();
options.addOption( option );



回答2:


Use the (inner) class Option.Builder as in

Option option = Option.builder("a")
 .required(true)
 .longOpt("arg-name")
 .build();

Cf. Option.Builder Java-Doc. I.e. the static builder() method of Option returns an Option.Builder and the trailing call to build() gives you an Option.



来源:https://stackoverflow.com/questions/35050704/apache-commons-cli-replacement-for-deprecated-optionbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!