Apache Commons CLI: replacement for deprecated OptionBuilder?

Mark Harrison picture Mark Harrison · Jan 28, 2016 · Viewed 9.5k times · Source

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

Answer

Julien Revault d'A... picture Julien Revault d'A... · Jan 28, 2016

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