DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Processing Command Line Arguments in Java

Processing Command Line Arguments in Java

Gary Sieling user avatar by
Gary Sieling
·
Jun. 26, 13 · Java Zone · Interview
Like (0)
Save
Tweet
16.51K Views

Join the DZone community and get the full member experience.

Join For Free

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");
    server.deleteByQuery("*:*");
  }
}
Command (computing) Java (programming language) Processing

Published at DZone with permission of Gary Sieling, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • Why Is Software Integration Important for Business?
  • How to Minimize Software Development Cost
  • What Is ERP Testing? - A Brief Guide

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo