Commons CLI API - Option.Builder Class

Q

How to use org.apache.commons.cli.Option.Builder class to define options?

✍: FYIcenter.com

A

Commons CLI API offers 3 ways to define options:

1. Define and add an option in a single addOption() call - For example:

import org.apache.commons.cli.Options;
      options.addOption("i", "input", true, "Input string");

2. Define an option with Option class first - The Option class gives you more control on how the option should behave. For example:

import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
      Option input = new Option("i", "input", true, "Input string");
      options.addOption(input);

3. Define an option with Option.Builder class first - The Option.Builder class gives you full control on how the option should behave. For example:

import org.apache.commons.cli.Option;
import org.apache.commons.cli.Option.Builder;
import org.apache.commons.cli.Options;
      builder = Option.builder("i");
      builder = builder.longOpt("input");
      builder = builder.desc("Input string");
      builder = builder.hasArg(true);
      builder = builder.argName("name");
      Option input = builder.build();
      options.addOption(input);

Here is good Java example of using org.apache.commons.cli.Option class to define options with Commons CLI API, ShortOptionTest.java:

// Copyright (c) 2018 FYIcenter.com
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Option.Builder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.UnrecognizedOptionException;

public class OptionBuilderTest {
   public static void main(String[] args) throws Exception {
      Options options = new Options();

      // Define a short option: -h
      Option.Builder builder = Option.builder("h");
      builder = builder.desc("Print this help message");
      Option help = builder.build();
      options.addOption(help);

      // Define a long option: --version -version -v
      builder = Option.builder("v");
      builder = builder.desc("Print version info");
      builder = builder.longOpt("version");
      Option version = builder.build();
      options.addOption(version);
      
      // Define a short option with argument: -n 3
      builder = Option.builder("n");
      builder = builder.desc("Number of output lines");
      builder = builder.hasArg(true);
      Option n = builder.build();
      options.addOption(n);

      // Define a long option with argument: --input ..., -input ..., -i ...
      builder = Option.builder("i");
      builder = builder.longOpt("input");
      builder = builder.desc("Input string");
      builder = builder.hasArg(true);
      builder = builder.argName("name");
      Option input = builder.build();
      options.addOption(input);

      try {
         // Parse options
         CommandLineParser parser = new DefaultParser();
         CommandLine cmd = parser.parse(options, args);
         
         int lines = 1; 
         if (cmd.hasOption("n")) {
            lines = Integer.parseInt(cmd.getOptionValue("n"));
         }
         
         if (cmd.hasOption("h")) {
            help(options);
         } else if (cmd.hasOption("version")) {
            System.out.println("OptionClassTest version 1.0.0");
         } else if (cmd.hasOption("input")) {
            for (int i=0; i<lines; i++) {
               System.out.println("Hello "+cmd.getOptionValue("input"));
            }
         }
      } catch (UnrecognizedOptionException e) {
         System.out.println("Invalid options: "+e.getOption());
         help(options);
      }
   }

   public static void help(Options options) {
      // Print options
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("OptionBuilderTest", options);
   }
}

You can compile and run it with commons-cli-1.4.jar:

C:\fyicenter>javac -cp C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionBuilderTest.java

C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionBuilderTest -h
usage: OptionBuilderTest
 -h                 Print this help message
 -i,--input <name>   Input string
 -n <arg>           Number of output lines
 -v,--version       Print version info

C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionBuilderTest --version
OptionBuilderTest version 1.0.0

C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionBuilderTest -i world
Hello world

C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionBuilderTest -i world -n 3
Hello world
Hello world
Hello world

 

FAQ for Apache Commons CLI JAR Library

Commons CLI API - Option Class

Using commons-cli.jar in Java Programs

⇑⇑ FAQ for Apache Commons CLI JAR Library

2020-05-15, 381👍, 0💬