Home » Core Java » apache » commons » cli » BasicParser » org.apache.commons.cli.BasicParser Example

About Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

org.apache.commons.cli.BasicParser Example

Basic Parser is for Command Line Processing. Command Line Processing has three stages definition, parsing and interrogation. Options are defined for command line processing. Command Line interface uses the options class as a container for option instances. To create options, there are two ways. The constructors and factory methods defined in options are the two methods of creating optios.

Definition stage defines the options instance. The parsing stage parses the options and the result is command line instance. Interrogation stage is  using the options for application data.

1. Source Code Example

The example below has the options created for the basic parser to parse and process them as application data.


package com.architectcorner.cli.parsing;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

 * @author Bhagvan Kommadi
 * Basic Parser example for parsing application data arguments
public class BasicParserExample extends BasicParser{

	 * @param args
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BasicParserExample exampleParser = new BasicParserExample();
		Options options = new Options()
        .addOption("a", "enable-a", false, "turn on or off")
        .addOption("b", "bfile", true, "set the value of [b]")
        .addOption("c", "copt", false, "turn on or off");
		String[] parserArgs = new String[] { "-a",
                "-b", "toast",
                "foo", "bar" };
		 CommandLine commandLine = exampleParser.parse(options,parserArgs);
		catch(ParseException parseException)
			System.out.println("Exception "+parseException.getMessage());
	 * flatten the options and arguments string
	 * @param arg0 options
	 * @param arg1 argument string
	 * @param arg2 boolean flag
	 * @return string array of flattened arguments
	protected String[] flatten(Options arg0, String[] arg1, boolean arg2) {
		// TODO Auto-generated method stub
		return super.flatten(arg0,arg1,arg2);

Basic parser can be used to define application data options and can be parsed for application data processing.


Basic Parser is defined extending the Parser  class and can be used for command line interface processing.

You can download the source code of the example here: BasicParserExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments