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 > Life is Too Short to Parse Command Line Parameters

Life is Too Short to Parse Command Line Parameters

Mitch Pronschinske user avatar by
Mitch Pronschinske
·
Jul. 20, 10 · Java Zone · Interview
Like (0)
Save
Tweet
13.69K Views

Join the DZone community and get the full member experience.

Join For Free
Recently, DZone MVB Cedric Beust unveiled JCommander, a tool he developed that takes away the manual labor of command line parsing.  Just six days after posting his announcement for JCommander 1.0, he's already got an expanded 1.1 version out.  New features include simple internationalization, type converters, and password parameters.

JCommander is a tiny Java framework that works by taking annotated fields with the descriptions of your options and then parsing command line parameters on its own.

Annotated Fields
import com.beust.jcommander.Parameter;

public class JCommanderTest {
@Parameter
public List<String> parameters = Lists.newArrayList();

@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
public Integer verbose = 1;

@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups;

@Parameter(names = "-debug", description = "Debug mode")
public boolean debug = false;
}
Telling JCommander to Parse
JCommanderTest jct = new JCommanderTest();
String[] argv = { "-log", "2", "-groups", "unit", "a", "b", "c" };
new JCommander(jct, argv);

Assert.assertEquals(jct.verbose.intValue(), 2);
Any type of field can be used to represent your parameters (Sting, Integer, Boolean,  etc.).  Non-basic types (files, host names, lists, etc.) can be written through type converters, which are new in 1.1.  JCommander comes with several common converters out of box.  You can write a type converter by implementing this interface:
public interface IStringConverter<T> {
T convert(String value);
}
Beust shows an example of a converter that turns a string into a FIle:
public class FileConverter implements IStringConverter<File> {
@Override
public File convert(String value) {
return new File(value);
}
}
Finally, you just declare your field with the proper type and specify the converter as an attribute:
@Parameter(names = "-file", converter = FileConverter.class)
File file;
For simple internationalization in 1.1, you can specify the bundle for use on your parameter class with the @ResourceBundle annotation.  JCommander will resolve your string with the default locale.

Finally, if one of your parameters is a password or a value that you want to keep private, you can declare it as a password type and JCommander will ask you to enter the password into the console so that you will be prompted for it whenever you run your program.  

Beust got a couple comments on his post voicing support for JCommander.  One noted that args4j does something similar but Beust says, "I'm certainly not claiming that the idea is original, plenty of people have pointed out prior similar frameworks (some dating back to 2005), some that I already knew, others I didn't. I'm just surprised that such an approach hasn't become a standard to parse command line parameters, so I thought I'd gather all the best ideas I've heard so far, write my own framework."

Download JCommander from Maven, GitHub, or JAR file.
Command (computing) Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Lean Software Development
  • Message Queuing and the Database: Solving the Dual Write Problem
  • Cloud-Based Integrations vs. On-Premise Models
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle

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