Over a million developers have joined DZone.

Processing Command Line Arguments in Java

DZone's Guide to

Processing Command Line Arguments in Java

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Rather than parsing command line arguments yourself, Apache has a nice library to do it for you, called Apache Commons CLI. It has a few different options, for various flavors of parsing, although this example demonstrates the two most common use cases (I think) – a flag, and setting a value.

The nice thing about this is it warns you if you specify incorrect arguments, so you can avoid worrying about that ever again.

import org.apache.commons.cli.*;
public class ArtEtl {
  public static void main(String[] args) throws Exception {
  Options options = new Options();
  options.addOption("d", false, "Delete records"); // does not have a value
  options.addOption("c", true, "CSV Repository"); // has a value
  CommandLineParser parser = new PosixParser();
  CommandLine cmd = parser.parse( options, args);
  if (cmd.hasOption("d")) {
    System.out.println("Clearing index");

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}