Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Command-Line Interfaces (Part 18): JCLAP

DZone's Guide to

Java Command-Line Interfaces (Part 18): JCLAP

Time to look at another CLI library that aims to make creating command-line interfaces for Java applications easier! In this entry, we take a look at the JCLAP library.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Giles Winstanley's JCLAP (Java Command-Line Argument Parser) is the eighteenth library covered in this series of posts on Java-based command line processing libraries. This post's examples are based on JCLAP 1.4, which requires Java 8. The main JCLAP page states, "JCLAP helps Java developers to create simple-to-use command-line interfaces for their applications."

The "definition" stage is accomplished with JCLAP via invocation of "addXXXXXOption" methods on the CLAParser object. This post's example, as was the case for examples in the earlier posts in this series, defines two command line options, one for file path and name and one for enabling verbosity. The next code listing demonstrates how to use JCLAP to define these two command line options (full code listing is available on GitHub).

"Definition" Stage With JCLAP

final CLAParser parser = new CLAParser();
final Option<String> fileNameOption
   = parser.addStringOption("f", "file", "Path/name of the file.", 1, 1);
final Option<Boolean> verbosityOption
   = parser.addBooleanOption("v", "verbose", "Verbosity enabled?");


The code listing just shown demonstrates that JCLAP supports long and short argument names, the ability to provide a description, and the ability to designate the minimum and maximum number of occurrences of each argument.

The "parsing" stage is implemented via JCLAP with a single invocation of the method CLAParser.parse(String[]), though that method does throw the checked exception OptionException.

"Parsing" Stage With JCLAP

parser.parse(arguments);


The "interrogation" stage is implemented in JCLAP in different ways, but the approach I use here is to use one of the overloaded CLAParser.getOptionValue() methods.

"Interrogation" Stage With JCLAP

out.println("File path/name is " + parser.getOptionValue(fileNameOption));
out.println("Verbosity is " + (parser.getOptionValue(verbosityOption) != null));


JCLAP also supports automatic usage statement creation. The next code listing demonstrates invoking one of the overloaded CLAParser.printUsage() methods in the block associated with catching the checked OptionException.

Automatic Usage Statement With JCLAP

catch (OptionException optionException)
{
   out.println("Exception: " + optionException);
   parser.printUsage(out, true);
}


The two screen snapshots that follow depict the code examples in action. The first screen snapshot shows the JCLAP-generated usage statement when no arguments are provided. The second image shows the "happy path" applying the long and short flag names for the two arguments.

Image title

Image title


There are characteristics of snaq.net JCLAP to consider when selecting a framework or library to help with command-line parsing in Java.

  • JCLAP is open source with a "BSD-style license" described on the project page.
  • The jclap-1.4.jar JAR file is approximately 46 KB in size.
  • Different versions of JCLAP are designed for different versions of Java. 
  • JCLAP's author has offered potential support and bug fixes as requested by email at support@snaq.net.
  • JCLAP provides some support for internationalization and localization.

JCLAP (Java Command-Line Argument Parser) is a small library with a commercial-friendly license that has been updated in recent years to use Java 8 features. JCLAP's author has written on the project's main page that "JCLAP is by no means unique, and many similar utilities are available both for free and commercially." The author further explains that "So many similar solutions now exist that it seems redundant to have yet another, but having already created JCLAP it seems beneficial to make it publicly available."

Additional References

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,cli ,parser ,jclap ,tutorial

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}