Over a million developers have joined DZone.

Java Command-Line Interfaces (Part 28): getopt4j

DZone's Guide to

Java Command-Line Interfaces (Part 28): getopt4j

In this installment of reviewing Java libraries for parsing command-line options, we take a look at one that strives to do it in the GNU style, similar to C's getopt().

· 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!

The page for getopt4j describes this as "a library to parse command line arguments according to the GNU style." The page then introduces getopt4j: "The 'getopt4j' library is designed to parse the command line options in the same manner as the C getopt() function in glibc (the GNU C runtime library). It attempts to do this in a simpler, more Java-centric manner than the original product." This post describes the use of getopt4j to parse command line options in the same manner as was done for the libraries covered in the earlier 27 posts in this series.

The "definition" stage is accomplished in getopt4j via instances of CLOptionDescriptor as demonstrated in the next code listing (full source code is available on GitHub).

"Definition" Stage With getopt4j

final CLOptionDescriptor fileDescriptor
   = new CLOptionDescriptor("file",
      "Path and name of file.");
final CLOptionDescriptor verboseDescriptor
   = new CLOptionDescriptor("verbose",
      "Is verbosity enabled?");
final CLOptionDescriptor[] optionsDefinitions
   = new CLOptionDescriptor[]{fileDescriptor, verboseDescriptor};

As shown in the above code, the instances of CLOptionDescriptor are placed in an array to be presented to the getopt4j parser.

The "parsing" stage is achieved in getopt4j via instantiation of the CLArgsParser class. The constructor of that class accepts the command line arguments in the String[] array and the array of CLOptionDescriptor instances representing the options' definitions. This is shown in the next code listing.

"Parsing" Stage With getopt4j

final CLArgsParser parser = new CLArgsParser(arguments, optionsDefinitions);

The "interrogation" stage in getopt4j is accomplished by retrieving a List<CLOption> via invocation of the method getArguments() on the CLArgsParser instance. Each instance of CLOption can be queried by its getId() method to acquire the parsed parameter by its "short" name ('f' or 'v' in this example). Once the appropriate instance of CLOption has been found via its getId() method, that same instance of CLOption will provide the value associated on the command line with that option via a call to the CLOption's method getArgument() method. This "interrogation" process is demonstrated in the next code listing.

"Interrogation" Stage With getopt4j

String filePathAndName = null;
boolean verbose = false;
final List<CLOption> options = parser.getArguments();
for (final CLOption option : options)
      case 'f' :
         filePathAndName = option.getArgument();
      case 'v' :
         verbose = true;

out.println("File path/name is '" + filePathAndName + "' and verbosity is " + verbose);

The getopt4j library makes it easy to request usage/help information by passing the array of CLOptionDescriptor instances to the static method CLUtil.describeOptions(CLOptionDescriptor[]). This is demonstrated in the next code listing, a couple of lines of code called when it is detected that the file path/name has not been provided.

"Usage" Statement With getopt4j

if (filePathAndName == null)
   out.println("ERROR: The file path/name option is required but was not provided.\n\n"
      + CLUtil.describeOptions(optionsDefinitions));

The first of the next two screen snapshots depicts the automatically generated "usage" statement that the code is able to invoke when the required "file option is not specified. The second image depicts various combinations of the "file" and "verbose" long and short option names being used.

Image title

Image title

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

The getopt4j library provides GNU C getopt()-like functionality and APIs with Java style.

Additional References

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.

command line options ,java ,cli ,gnu ,getopt3j

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}