22

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());
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

2 Answers2

31

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 );
  • Note: if you're having trouble with this via groovy, it might be because you're using java 7. When I used a java 7 runtime with commons-cli-1.4.jar I'd get `signature of method: static org.apache.commons.cli.Option.builder() is applicable for argument types` – Sridhar Sarnobat Mar 14 '17 at 18:19
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.

Hille
  • 4,096
  • 2
  • 22
  • 32