- Commons CLI - Home
- Commons CLI - Overview
- Commons CLI - Environment Setup
- Commons CLI - First Application
- Commons CLI - Option Properties
- Commons CLI - Boolean Option
- Commons CLI - Argument Option
- Commons CLI - Properties Option
- Commons CLI - Posix Parser
- Commons CLI - GNU Parser
- Commons CLI - Usage Example
- Commons CLI - Help Example
- Apache Commons CLI Resources
- Commons CLI - Quick Guide
- Commons CLI - Useful Resources
- Commons CLI - Discussion
Apache Commons CLI - First Application
Introduction
In order to create a console based application using Apache Commons CLI, we define stages.
Definition Stage − Used to define the options used as command line arguments.
Parsing Stage − Used to define the parser and parse the arguments using options.
Interrogation Stage − Used to check the arguments passed and perform the business logic accordingly.
Definition Stage
Define the Options to be used as command line arguments.
// create Options object
Options options = new Options();
// add option "-a"
options.addOption("a", false, "add numbers");
Parsing Stage
Define the CommandLineParser. We're using DefaultParser object here.
// Create a parser CommandLineParser parser = new DefaultParser(); // parse the options passed as command line arguments CommandLine cmd = parser.parse( options, args);
Interrogation Stage
Check the options passed during execution and act accordingly.
// hasOptions checks if option is present or not
if(cmd.hasOption("a")) {
System.out.println("Sum of the numbers: " + getSum(args));
} else if(cmd.hasOption("m")) {
System.out.println("Multiplication of the numbers: " + getMultiplication(args));
}
Example - Creating First Console Application
Let's create a sample console based application, whose purpose is to get either sum of passed numbers or multiplication of passed numbers based on the options 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.Options;
import org.apache.commons.cli.ParseException;
public class CLITester {
public static void main(String[] args) throws ParseException {
// ***Definition Stage***
// create Options object
Options options = new Options();
// add option "-a"
options.addOption("a", false, "add numbers");
// add option "-m"
options.addOption("m", false, "multiply numbers");
// ***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));
} else if(cmd.hasOption("m")) {
System.out.println("Multiplication of the numbers: " + getMultiplication(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;
}
public static int getMultiplication(String[] args) {
int multiplication = 1;
for(int i = 1; i < args.length ; i++) {
multiplication *= Integer.parseInt(args[i]);
}
return multiplication;
}
}
Output
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
Run the file, while passing -m as option and numbers to get the multiplication of the numbers as result −
java CLITester -m 1 2 3 4 5 Multiplication of the numbers: 120