"Simple apache commons CLI example. Java command line arguments parsing."

Date:July 8th, 2014
Comments: 4
  • Join date: 11-30-99
  • Posts: 12
View Counter:
Rate it
  • Which features are most important for perfect CI tool?

    View Results

    Loading ... Loading ...
  • bodytext bodytext bodytext
[i]. Apache commons CLI is simple, yet powerful and fun to use way to implement command line parsing in Java.

Following Apache commons CLI example focuses on simplicity so you can copy paste, modify and reuse this code snippet.

You will need java installed and the CLI jar file, I guess 1.2 version would be nice. If you use maven, I also posted simple pom.xml file.

Main file just an entry point Main.java :

public class Main {
 public static void main(String[] args) {
  new Cli(args).parse();

Cli file does the heavy lifting Cli.java :

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class Cli {
 private static final Logger log = Logger.getLogger(Cli.class.getName());
 private String[] args = null;
 private Options options = new Options();

 public Cli(String[] args) {

  this.args = args;

  options.addOption("h", "help", false, "show help.");
  options.addOption("v", "var", true, "Here you can set parameter .");


 public void parse() {
  CommandLineParser parser = new BasicParser();

  CommandLine cmd = null;
  try {
   cmd = parser.parse(options, args);

   if (cmd.hasOption("h"))

   if (cmd.hasOption("v")) {
    log.log(Level.INFO, "Using cli argument -v=" + cmd.getOptionValue("v"));
    // Whatever you want to do with the setting goes here
   } else {
    log.log(Level.SEVERE, "MIssing v option");

  } catch (ParseException e) {
   log.log(Level.SEVERE, "Failed to parse comand line properties", e);

 private void help() {
  // This prints out some help
  HelpFormatter formater = new HelpFormatter();

  formater.printHelp("Main", options);

You can use this pom with maven to get the jar and things running pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



compile, run it. Here are output examples on console:

diuw@host /cygdrive/D/Users/diuw/workspace/Cli
$ java -classpath "commons-cli-1.2.jar;." Main -v=test
Jul 08, 2014 6:36:01 PM Cli parse
SCHWERWIEGEND: Failed to parse comand line properties
org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: -v=test
 at org.apache.commons.cli.Parser.processOption(Parser.java:363)
 at org.apache.commons.cli.Parser.parse(Parser.java:199)
 at org.apache.commons.cli.Parser.parse(Parser.java:85)
 at Cli.parse(Cli.java:30)
 at Main.main(Main.java:4)

usage: Main
-h,--help show help.
-v,--var <arg>   Here you can set parameter .

diuw@host /cygdrive/D/Users/diuw/workspace/Cli
$ java -classpath "commons-cli-1.2.jar;." Main -v test
Jul 08, 2014 6:36:05 PM Cli parse
INFORMATION: Using cli argument -v=test 

Or use the maven thing to run it…

Be Sociable, Share!
Interesting entry?
VN:F [1.8.5_1061]
Rating: +4 (from 18 votes)
11 votes 'YES'  7 votes 'NO'

LEARN MORE (amazon bookstore)



Be Sociable, Share!



4 Responses to “Simple apache commons CLI example. Java command line arguments parsing.”

  1. Jacob M. says:

    thanks this was very helpfull

  2. Russ says:

    Thank you a nice article. It helped me when I was looking at adding a CLI to my application.

Leave a Reply