Home United States USA — software Java Command-Line Interfaces (Part 17): jw-options

Java Command-Line Interfaces (Part 17): jw-options

360
0
SHARE

Explore a simple tool to process command-line arguments: jw-options. It’s particularly useful for older versions of Java and is open source for all to use.
The “definition” stage with “jw-options” is achieved with its Options and OptionSet classes. This is demonstrated in the next code listing (full code listing is available on GitHub and the example here is similar to those used in earlier posts in this series).
The code listing just shown demonstrates using a couple of OptionSet ‘s overloaded addOption methods. For setting up the option for file path and name ( -f), a four-argument version is called with the single-letter of the flag (“f”), the separator between the flag and its argument (a space), and the number of times the flag should be expected (exactly one occurrence). The second argument for verbosity (“-v”) is set up by calling the two-argument version of addOption that specifies the flag’s character (“v”) and its number of expected occurrences (zero occurrences or single occurrence).
The “parsing” stage is achieved in “jw-options” by invoking Options ‘s check method. This method can also be used, as its name suggests, to check the accuracy of the arguments. This is demonstrated in the next code listing.
In the “parsing” example just shown, the Options class’s method getCheckErrors() was used to access the errors in the parsed parameters that led to the Options.check method returning false .
The “interrogation” stage with “jw-options” is demonstrated in the next code listing.
The “interrogation” example demonstrates using OptionSet ‘s getOption method to access the option representing the “-f” option and then calls its getResultValue(0) method to access the first (and only in this case) value associated with that “-f” flag. The second line in that example inquires simply whether the “-v” flag has been specified or not (and does not worry about or expect a value to be associated with that flag) via use of the OptionSet ‘s method isSet.
A screen snapshot is shown next to demonstrate the code shown so far that uses “jw-options.” The image shows the messages reported when expected command line arguments are not provided and ends with two examples using the command line flags as intended.
There are characteristics of “jw-options” to consider when selecting a framework or library to help with command-line parsing in Java.
The “jw-options” “library” discussed in this post is most likely to interest those who need to use a command line processing library with an older version of Java or who are interested in it in an academic sense. Because this “library” is described in detail in the associated JavaWorld article and because it’s open source, one can peruse the code and review the article to see how it accomplishes the command line parsing and why it uses that approach. Given that the license for “jw-options” is not obvious and given that this is a relatively “old” library that doesn’t seem to receive updates, it is likely that most Java developers would prefer some of the alternate libraries covered in this series over “jw-options” in many cases.

Continue reading...