Java Command-Line Interfaces (Part 15): Jargo
Java Command-Line Interfaces (Part 15): Jargo
We take a look at another CLI library that can help you deal with command line arguments and options. Meet Jargo and its focus on immutability and type safety.
Join the DZone community and get the full member experience.Join For Free
Get the Edge with a Professional Java IDE. 30-day free trial.
Jargo is defined on its main GitHub page as "a tool to ease the handling of program arguments/options." That page provides a Rationale for another command line processing library when so many others already exist and the top of that list is, "Because type-safety, immutability, and readability matters."
Jargo's options "definition" stage uses generic typed instances of the Argument class. These instances of
Argument are created via static methods on the Arguments class to establish the type and then using builder-style methods to describe the option. This is demonstrated in the next screen snapshot which depicts definition of options for file path/name and verbosity (full code listing is available on GitHub).
"Definition" Stage With Jargo
final Argument<String> filePathAndName = stringArgument().description("Path and name of file.") .names("--file", "-f") .required() .build(); // Use optionArgument() instead of booleanArgument() to avoid need // to specify true or false as arguments to --verbose/-v option final Argument<Boolean> verbose = optionArgument("--verbose", "-v") .description("Enables verbosity.") .names("--verbose", "-v") .defaultValue(false) .build();
optionArgument() methods shown above are called on the statically imported (not shown)
Arguments class. The
optionArgument() method needed to be used for the verbosity flag to avoid being required to explicitly state
false after the verbosity flag.
The "parsing" stage is implemented with the class CommandLineParser and its fluent API methods as shown in the next code listing.
final ParsedArguments parsedArguments = CommandLineParser.withArguments(filePathAndName, verbose) .parse(arguments);
The instance of ParsedArguments provided by the
CommandLineParser can be used for the "interrogation" stage. This is accomplished by invoking the "get" method on the
ParsedArguments instance and passing it the appropriate
Argument instance. The next code listing demonstrates this.
"Interrogation" Stage With Jargo
out.println("File path/name is '" + parsedArguments.get(filePathAndName) + "' and verbosity is set to '" + parsedArguments.get(verbose) + "'.");
The following screen snapshots depict use of Jargo. The first screen snapshot demonstrates the exception stack trace that occurs when a required option is not specified and the second screen snapshot demonstrates the long and short option names being used.
The stack trace shown in the first screen snapshot is not the nicest way to notify the user that a required option was not specified. Jargo allows a nicer message to be returned by catching the ArgumentException and calling its
getMessageAndUsage() method. The code for this can be seen on GitHub and the results are shown in the next screen snapshot.
The screen snapshot demonstrates that the information provided in the instantiation of the
Arguments is displayed. Jargo also allows an exception to be explicitly thrown to provide this information when a "help" argument is specified. This makes use of the static method
helpArgument() on the
Arguments class and an example of its usage is included in the GitHub code listing.
There are characteristics of Jargo to consider when selecting a framework or library to help with command-line parsing in Java.
- Jargo is open source and is licensed under the Apache License, Version 2.0.
- Jargo's jargo-0.4.1.jar is approximately 177 KB in size, but it has a runtime dependency on the much larger Guava library.
- The Guava dependency is an intentional decision as described in Jargo's Rationale: "Because I love Guava and wanted an argument parsing library well integrated with it (more to come in this department)."
- This is obviously not an issue for the many applications that use Guava, but could be an issue for those wanting a command line processing Java-based library for a simple application that did not otherwise use Guava.
- Jargo uses strongly typed API invocations to programmatically configure expected command line options rather than using annotations and/or reflection.
- In a field with so many Java-based command line processing libraries available, Jargo is most likely to be a significant contender for a developer who desires all of the attributes of a command line processing library that Jargo's Rationale lists in explanation of why another library in this crowded space was developed.
Jargo is an easy-to-use library for processing command line options in Java and makes use of generic-typed classes and type-specific static methods to enforce type safety of command line options. Jargo requires Guava to run and so is best suited for applications already using Guava. A developer is likely to seriously consider Jargo over other alternate Java-based command line processing libraries if the items in the Jargo Rationale are all important to that developer.
Published at DZone with permission of Dustin Marx , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.