@@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2626import java .util .List ;
2727import java .util .Objects ;
2828import java .util .Properties ;
29+ import java .util .function .Consumer ;
2930import java .util .function .Supplier ;
3031
3132/**
@@ -47,12 +48,22 @@ public class CommandLine implements Serializable {
4748 */
4849 public static final class Builder {
4950
51+ /**
52+ * Prints an Option to {@link System#out}.
53+ */
54+ static final Consumer <Option > DEPRECATED_HANDLER = o -> System .out .println (o );
55+
5056 /** The unrecognized options/arguments */
5157 private final List <String > args = new LinkedList <>();
5258
5359 /** The processed options */
5460 private final List <Option > options = new ArrayList <>();
5561
62+ /**
63+ * Deprecated Option handler.
64+ */
65+ private Consumer <Option > deprecatedHandler = DEPRECATED_HANDLER ;
66+
5667 /**
5768 * Adds left-over unrecognized option/argument.
5869 *
@@ -82,15 +93,30 @@ public Builder addOption(final Option opt) {
8293 }
8394
8495 /**
85- * Returns the new instance.
96+ * Creates the new instance.
8697 *
8798 * @return the new instance.
8899 */
89100 public CommandLine build () {
90- return new CommandLine (args , options );
101+ return new CommandLine (args , options , deprecatedHandler );
102+ }
103+
104+ /**
105+ * Sets the deprecated option handler.
106+ *
107+ * @param deprecatedHandler the deprecated option handler.
108+ * @return this.
109+ * @since 1.7.0
110+ */
111+ public Builder setDeprecatedHandler (final Consumer <Option > deprecatedHandler ) {
112+ this .deprecatedHandler = deprecatedHandler ;
113+ return this ;
91114 }
92115 }
93116
117+ /** The serial version UID. */
118+ private static final long serialVersionUID = 1L ;
119+
94120 /**
95121 * Creates a new builder.
96122 *
@@ -101,28 +127,34 @@ public static Builder builder() {
101127 return new Builder ();
102128 }
103129
104- /** The serial version UID. */
105- private static final long serialVersionUID = 1L ;
106-
107130 /** The unrecognized options/arguments */
108131 private final List <String > args ;
109132
110133 /** The processed options */
111134 private final List <Option > options ;
112135
136+ /**
137+ * The deprecated option handler.
138+ * <p>
139+ * If you want to serialize this field, use a serialization proxy.
140+ * </p>
141+ */
142+ private final transient Consumer <Option > deprecatedHandler ;
143+
113144 /**
114145 * Creates a command line.
115146 */
116147 protected CommandLine () {
117- this (new LinkedList <>(), new ArrayList <>());
148+ this (new LinkedList <>(), new ArrayList <>(), Builder . DEPRECATED_HANDLER );
118149 }
119150
120151 /**
121152 * Creates a command line.
122153 */
123- private CommandLine (final List <String > args , final List <Option > options ) {
154+ private CommandLine (final List <String > args , final List <Option > options , final Consumer < Option > deprecatedHandler ) {
124155 this .args = Objects .requireNonNull (args , "args" );
125156 this .options = Objects .requireNonNull (options , "options" );
157+ this .deprecatedHandler = deprecatedHandler ;
126158 }
127159
128160 /**
@@ -530,13 +562,15 @@ public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws
530562 }
531563
532564 /**
533- * Tests to see if an option has been set .
565+ * Handles deprecated options .
534566 *
535- * @param opt character name of the option.
536- * @return true if set, false if not.
567+ * @param option a deprecated option.
537568 */
538- public boolean hasOption (final char opt ) {
539- return hasOption (String .valueOf (opt ));
569+ private void handleDeprecated (final Option option ) {
570+ if (deprecatedHandler != null ) {
571+ deprecatedHandler .accept (option );
572+ }
573+
540574 }
541575
542576 /**
@@ -557,6 +591,16 @@ public boolean hasOption(final char opt) {
557591 * return buf.toString(); }
558592 */
559593
594+ /**
595+ * Tests to see if an option has been set.
596+ *
597+ * @param opt character name of the option.
598+ * @return true if set, false if not.
599+ */
600+ public boolean hasOption (final char opt ) {
601+ return hasOption (String .valueOf (opt ));
602+ }
603+
560604 /**
561605 * Tests to see if an option has been set.
562606 *
@@ -615,6 +659,9 @@ private Option resolveOption(final String opt) {
615659 if (actual != null ) {
616660 for (final Option option : options ) {
617661 if (actual .equals (option .getOpt ()) || actual .equals (option .getLongOpt ())) {
662+ if (option .isDeprecated ()) {
663+ handleDeprecated (option );
664+ }
618665 return option ;
619666 }
620667 }
0 commit comments