Skip to content

Commit c4e1fbd

Browse files
committed
introduct to Options the tracking of deprecated short and long opts
1 parent c4d712f commit c4e1fbd

6 files changed

Lines changed: 145 additions & 1 deletion

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ private void processPropertiesFromValues(final Properties props, final List<Stri
587587

588588
/**
589589
* Retrieves the option object given the long or short option as a String
590+
* It also consults any deprecated option names.
590591
*
591592
* @param opt short or long name of the option, may be null.
592593
* @return Canonicalized option.
@@ -598,6 +599,14 @@ private Option resolveOption(final String opt) {
598599
if (actual.equals(option.getOpt()) || actual.equals(option.getLongOpt())) {
599600
return option;
600601
}
602+
else if (actual.equals(option.getDeprecatedOpt() )) {
603+
System.out.println("Using deprecated option name '" + actual + "' instead of proper name '" + option.getOpt() + "'");
604+
return option;
605+
}
606+
else if (actual.equals(option.getDeprecatedLongOpt() )) {
607+
System.out.println("Using deprecated long option '" + actual + "' instead of proper long name '" + option.getLongOpt() + "'");
608+
return option;
609+
}
601610
}
602611
}
603612
return null;

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4242
*/
4343
public class Option implements Cloneable, Serializable {
4444

45+
4546
/**
4647
* Builds {@code Option} instances using descriptive methods.
4748
* <p>
@@ -65,6 +66,12 @@ public static final class Builder {
6566
/** The long representation of the option. */
6667
private String longOption;
6768

69+
/** Specifies if the option had a previous deprecated long representation of the option. */
70+
private String deprecatedLongOption;
71+
72+
/** Specifies if the option had a previous deprecated representation of the option. */
73+
private String deprecatedOption;
74+
6875
/** The name of the argument for this option. */
6976
private String argName;
7077

@@ -187,6 +194,29 @@ public Builder longOpt(final String longOpt) {
187194
return this;
188195
}
189196

197+
/**
198+
* Sets the deprecated long name of the Option.
199+
*
200+
* @param deprecatedLongOption the deprecated long name of the Option
201+
* @return this builder.
202+
*/
203+
public Builder deprecatedLongOption(final String deprecatedLongOption) {
204+
this.deprecatedLongOption = deprecatedLongOption;
205+
return this;
206+
}
207+
/**
208+
* Sets the deprecated long name of the Option.
209+
*
210+
* @param deprecatedOption the deprecated name of the Option
211+
* @return this builder.
212+
*/
213+
public Builder deprecatedOption(final String deprecatedOption) {
214+
this.deprecatedOption = deprecatedOption;
215+
return this;
216+
}
217+
218+
219+
190220
/**
191221
* Sets the number of argument values the Option can take.
192222
*
@@ -332,6 +362,12 @@ public static Builder builder(final String option) {
332362
/** The long representation of the option. */
333363
private String longOption;
334364

365+
/** Deprecated Long option */
366+
private String deprecatedLongOption;
367+
368+
/** Deprecated option */
369+
private String deprecatedOption;
370+
335371
/** The name of the argument for this option. */
336372
private String argName;
337373

@@ -368,6 +404,8 @@ private Option(final Builder builder) {
368404
this.argName = builder.argName;
369405
this.description = builder.description;
370406
this.longOption = builder.longOption;
407+
this.deprecatedLongOption = builder.deprecatedLongOption;
408+
this.deprecatedOption = builder.deprecatedOption;
371409
this.argCount = builder.argCount;
372410
this.option = builder.option;
373411
this.optionalArg = builder.optionalArg;
@@ -608,6 +646,14 @@ public String getOpt() {
608646
return option;
609647
}
610648

649+
public String getDeprecatedLongOpt() {
650+
return deprecatedLongOption;
651+
}
652+
653+
public String getDeprecatedOpt() {
654+
return deprecatedOption;
655+
}
656+
611657
/**
612658
* Gets the type of this Option.
613659
*
@@ -720,6 +766,24 @@ public boolean hasLongOpt() {
720766
return longOption != null;
721767
}
722768

769+
/**
770+
* Tests whether this Option has a deprecated long name.
771+
*
772+
* @return boolean flag indicating existence of a deprecated long name.
773+
*/
774+
public boolean hasDeprecatedLongOpt() {
775+
return deprecatedLongOption != null;
776+
}
777+
778+
/**
779+
* Tests whether this Option has a deprecated option name.
780+
*
781+
* @return boolean flag indicating existence of a deprecated option name.
782+
*/
783+
public boolean hasDeprecatedOption() {
784+
return deprecatedOption != null;
785+
}
786+
723787
/**
724788
* Tests whether this Option has any values.
725789
*
@@ -861,6 +925,24 @@ public void setLongOpt(final String longOpt) {
861925
this.longOption = longOpt;
862926
}
863927

928+
/**
929+
* Sets the deprecated long name of this Option.
930+
*
931+
* @param deprecatedLongOption the deprecated long name of this Option.
932+
*/
933+
public void setDeprecatedLongOption(final String deprecatedLongOption) {
934+
this.deprecatedLongOption = deprecatedLongOption;
935+
}
936+
937+
/**
938+
* Sets the deprecated ame of this Option.
939+
*
940+
* @param deprecatedOption the deprecated name of this Option.
941+
*/
942+
public void setDeprecatedOption(final String deprecatedOption) {
943+
this.deprecatedOption = deprecatedOption;
944+
}
945+
864946
/**
865947
* Sets whether this Option can have an optional argument.
866948
*

src/main/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public final class OptionBuilder {
3535
/** Long option */
3636
private static String longOption;
3737

38+
/** Deprecated Long option */
39+
private static String deprecatedLongOption;
40+
41+
/** Deprecated option */
42+
private static String deprecatedOption;
43+
3844
/** Option description */
3945
private static String description;
4046

@@ -104,9 +110,11 @@ public static Option create(final String opt) throws IllegalArgumentException {
104110
option = new Option(opt, description);
105111

106112
// set the option properties
107-
option.setLongOpt(longOption);
108113
option.setRequired(required);
114+
option.setLongOpt(longOption);
109115
option.setOptionalArg(optionalArg);
116+
option.setDeprecatedOption(deprecatedOption);
117+
option.setDeprecatedLongOption(deprecatedLongOption);
110118
option.setArgs(argCount);
111119
option.setType(type);
112120
option.setConverter(TypeHandler.getConverter(type));
@@ -234,6 +242,8 @@ private static void reset() {
234242
description = null;
235243
argName = null;
236244
longOption = null;
245+
deprecatedOption = null;
246+
deprecatedLongOption = null;
237247
type = String.class;
238248
required = false;
239249
argCount = Option.UNINITIALIZED;
@@ -350,10 +360,22 @@ public static OptionBuilder withValueSeparator(final char sep) {
350360
return INSTANCE;
351361
}
352362

363+
public static OptionBuilder withDeprecatedLongOpt(String deprecatedLongOption) {
364+
OptionBuilder.deprecatedLongOption = deprecatedLongOption;
365+
366+
return INSTANCE;
367+
}
368+
369+
public static OptionBuilder withDeprecatedOpt(String deprecatedOption){
370+
OptionBuilder.deprecatedOption = deprecatedOption;
371+
372+
return INSTANCE;
373+
}
353374
/**
354375
* private constructor to prevent instances being created
355376
*/
356377
private OptionBuilder() {
357378
// hide the constructor
358379
}
380+
359381
}

src/main/java/org/apache/commons/cli/Options.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public Options addOption(final Option opt) {
6666
if (opt.hasLongOpt()) {
6767
longOpts.put(opt.getLongOpt(), opt);
6868
}
69+
if (opt.hasDeprecatedLongOpt()) {
70+
longOpts.put(opt.getDeprecatedLongOpt(), opt);
71+
}
6972
// if the option is required add it to the required list
7073
if (opt.isRequired()) {
7174
if (requiredOpts.contains(key)) {
@@ -74,6 +77,9 @@ public Options addOption(final Option opt) {
7477
requiredOpts.add(key);
7578
}
7679
shortOpts.put(key, opt);
80+
if (opt.hasDeprecatedOption()) {
81+
shortOpts.put(opt.getDeprecatedOpt(), opt);
82+
}
7783
return this;
7884
}
7985

src/test/java/org/apache/commons/cli/DefaultParserTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,25 @@ public void testShortOptionQuoteHandlingWithStrip() throws Exception {
120120

121121
assertEquals("quoted string", cl.getOptionValue("b"), "Confirm -b \"arg\" strips quotes");
122122
}
123+
124+
@Test
125+
public void testUsingDeprecatedOptions() throws Exception {
126+
127+
options = new Options().addOption(
128+
Option.builder("a")
129+
.longOpt("enable-a")
130+
.hasArg(true)
131+
.deprecatedLongOption("old-enable-a")
132+
.deprecatedOption("z").build());
133+
134+
parser = DefaultParser.builder().build();
135+
136+
final String[] args = {"-a", "arg"};
137+
final CommandLine cl = parser.parse(options, args);
138+
139+
assertEquals("arg", cl.getOptionValue("a"), "normal name look up");
140+
assertEquals("arg", cl.getOptionValue("enable-a"), "normal long name look up");
141+
assertEquals("arg", cl.getOptionValue("z"), "deprecated name look up");
142+
assertEquals("arg", cl.getOptionValue("old-enable-a"), "deprecated long name look up");
143+
}
123144
}

src/test/java/org/apache/commons/cli/OptionBuilderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public void testSpecialOptChars() throws Exception {
158158
public void testTwoCompleteOptions() {
159159
//@formatter:off
160160
Option simple = OptionBuilder.withLongOpt("simple option")
161+
.withDeprecatedLongOpt("deprecated simple option")
162+
.withDeprecatedOpt("deprecated-s")
161163
.hasArg()
162164
.isRequired()
163165
.hasArgs()
@@ -168,6 +170,8 @@ public void testTwoCompleteOptions() {
168170

169171
assertEquals("s", simple.getOpt());
170172
assertEquals("simple option", simple.getLongOpt());
173+
assertEquals("deprecated-s", simple.getDeprecatedOpt());
174+
assertEquals("deprecated simple option", simple.getDeprecatedLongOpt());
171175
assertEquals("this is a simple option", simple.getDescription());
172176
assertEquals(simple.getType(), Float.class);
173177
assertTrue(simple.hasArg());

0 commit comments

Comments
 (0)