Apache Commons CLI - Option Properties



Option object is used to represent the Option passed to command line program. Following are various properties that an Option object possess.

Option Object Properties

Sr.No Name (Type) &amps; Description
1

opt (String)

Identification string of the Option.

2

longOpt (String)

Alias and more descriptive identification string.

3

description (String)

Description of the function of the option.

4

required (boolean)

Flag to check whether the option must appear on the command line.

5

arg (boolean)

Flag to check whether the option takes an argument.

6

args (boolean)

Flag to check whether the option takes more than one argument.

7

optionalArg (boolean)

Flag to check whether the option's argument is optional.

8

argName (String)

Name of the argument value for the usage statement.

9

valueSeparator (char)

The character value used to split the argument string.

10

type (Object)

Argument type.

11

value (String)

Option value.

12

values (String[])

Values of the option.

Example - Usage of Option Properties - Without Mandatory Argument

Let's create a sample console based application, whose purpose is to get sum of passed numbers based on the option used.

CLITester.java

package com.tutorialspoint;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CLITester {
   public static void main(String[] args) throws ParseException {
      // create Options object
      Options options = new Options();

      // Create Option object
      Option addOption = new Option("a", "Add Numbers");
      
      // argument is required
      addOption.setRequired(true);
      
      // add option "-a"
      options.addOption(addOption);
      
      // ***Parsing Stage***
      // Create a parser
      CommandLineParser parser = new DefaultParser();

      // parse the options passed as command line arguments
      CommandLine cmd = parser.parse( options, args);

      // ***Interrogation Stage***
      // hasOptions checks if option is present or not
      if(cmd.hasOption("a")) {
         System.out.println("Sum of the numbers: " + getSum(args));
      } 
   }
   public static int getSum(String[] args) {
      int sum = 0;
      for(int i = 1; i < args.length ; i++) {
         sum += Integer.parseInt(args[i]);
      }
      return sum;
   }   
}

Output

Run the file, without passing -a as option and CLI will throw MissingOptionException −

Exception in thread "main" org.apache.commons.cli.MissingOptionException: Missing required option: a
	at org.apache.commons.cli.DefaultParser.checkRequiredOptions(DefaultParser.java:344)
	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:770)
	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:815)
	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:797)
	at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:776)
	at com.tutorialspoint.CLITester.main(CLITester.java:29)

Run the file, while passing -a as option and numbers to get the sum of the numbers as result −

java CLITester -a 1 2 3 4 5
Sum of the numbers: 15
Advertisements