简体   繁体   中英

Apache Commons CLI: replacement for deprecated OptionBuilder?

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());

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 );

Use the (inner) class Option.Builder as in

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM