- The last option to create is the Java property and it is also created
+ The last option to create is the Java property, and it is also created
using the Option class' Builder.
- All the preperation is now complete and we are now ready to
+ All the preparation is now complete, and we are now ready to
parse the command line arguments.
If you also require to have a usage statement printed
then calling formatter.printHelp("ant", options, true)
- will generate a usage statment as well as the help information.
+ will generate a usage statement as well as the help information.
@@ -389,7 +389,7 @@ catch (ParseException exp) {
By in most cases the values on the command line are retrieved as Strings via the
commandLine.getOptionValue(key) command. However, it is possible for
the CLI library to convert the string into a different object. For example to specify
- that the "count" option should reutrn an Integer the following code could be used:
+ that the "count" option should return an Integer the following code could be used:
public static void main(String[] args) {
@@ -398,7 +398,7 @@ catch (ParseException exp) {
.desc("the number of things")
.type(Integer.class)
.build();
- Options options = new Options().addOption(cound);
+ Options options = new Options().addOption(count);
// create the parser
CommandLineParser parser = new DefaultParser();
try {
@@ -440,7 +440,7 @@ catch (ParseException exp) {
Additional types may be added to the automatic parsing system by calling TypeHandler.register(Class<T> clazz, Converter<T> converter).
The Class<T> can be any defined class. The converter is a function that takes a String argument and returns an instance of
- the class. Any expection thrown by the constructor will be caught and reported as a ParseException
+ the class. Any exception thrown by the constructor will be caught and reported as a ParseException
Conversions can be specified without using the TypeHandler class by specifying the converter
@@ -459,5 +459,154 @@ catch (ParseException exp) {
before deserialization begins.
+
+
+ Options may be marked as deprecated using ghe Option.builder.deprecated() method.
+ Additional information may be specified by passing a DeprecatedAttributes instance to the
+ deprecated method.
+
+ Usage of the deprecated option is announced when the presence of the option is checked
+ or the value of the option is retrieved. By default the announcement printed to Standard out.
+
+ The HelpFormatter output will be default show the description prefixed by "[Deprecated]"
+
+
+ The examples below will implement doSomething in the following code block.
+
+ public static void main(String[] args) {
+ Option n = Option.builder("n")
+ .deprecated(DeprecatedAttributes.builder()
+ .setDescription("Use '-count' instead")
+ .setForRemoval(true)
+ .setSince("now").get())
+ .hasArg()
+ .desc("the number of things")
+ .type(Integer.class)
+ .build();
+ Option count = Option.builder("count")
+ .hasArg()
+ .desc("the number of things")
+ .type(Integer.class)
+ .build();
+ Options options = new Options().addOption(n).addOption(count);
+
+ doSomething(options);
+ }
+
+
+
+
+
+ The usage announcement may be changed by providing a Consumer>Option<> to the
+ CommandLine.Builder.deprecatedHandler method.
+
+
+ ERROR: Option 'n': Deprecated for removal since now: Use '-count' instead
+ n=5
+
+
+ However, the first line would be printed on system err instead of system out.
+
+
+
+
+
+ If doSomething is implemented as:
+
+ void doSomething(Options options) {
+ HelpFormatter formatter = HelpFormatter.builder().get();
+ formatter.printHelp("Command line syntax:", options);
+ }
+
+ To output is
+
+ usage: Command line syntax:
+ -count <arg> the number of things
+ -n <arg> [Deprecated] the number of things
+
+
+
+ The display of deprecated options may be changed through the use of the
+ HelpFormatter.Builder.setShowDeprecated() method.
+
+
Calling HelpFormatter.Builder.setShowDeprecated(false) will disable the "[Deprecated]" tag.
+
Calling HelpFormatter.Builder.setShowDeprecated with a BiFunction<String, Option, String>
+ will use the output of the function as the description for the option.
+
+ As an example of the second case above, changing the implementation of doSomething to
+
+ void doSomething(Options options) {
+ BiFunction<String, Option, String> disp = (desc, option) -> String.format( "%s. %s", desc, option.getDeprecated().toString());
+ HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(disp).get();
+ formatter.printHelp("Command line syntax:", options);
+ }
+
+ changes the output to
+
+ usage: Command line syntax:
+ -count <arg> the number of things
+ -n <arg> the number of things. Deprecated for removal since now:
+ Use '-count' instead
+
+
+
+