Skip to content

Commit c63265b

Browse files
[CLI-331] Handle reporting of deprecated options when parameters are not String type. (#270)
* fixes * added additional testing * Indent --------- Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent c04de22 commit c63265b

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ public String[] getOptionValues(final Option option) {
405405
if (option == null) {
406406
return null;
407407
}
408+
if (option.isDeprecated()) {
409+
handleDeprecated(option);
410+
}
408411
final List<String> values = new ArrayList<>();
409412
for (final Option processedOption : options) {
410413
if (processedOption.equals(option)) {
@@ -498,6 +501,9 @@ public <T> T getParsedOptionValue(final Option option, final Supplier<T> default
498501
if (option == null) {
499502
return get(defaultValue);
500503
}
504+
if (option.isDeprecated()) {
505+
handleDeprecated(option);
506+
}
501507
final String res = getOptionValue(option);
502508
try {
503509
if (res == null) {
@@ -615,7 +621,11 @@ public boolean hasOption(final char opt) {
615621
* @since 1.5.0
616622
*/
617623
public boolean hasOption(final Option opt) {
618-
return options.contains(opt);
624+
boolean result = options.contains(opt);
625+
if (result && opt.isDeprecated()) {
626+
handleDeprecated(opt);
627+
}
628+
return result;
619629
}
620630

621631
/**
@@ -665,9 +675,6 @@ private Option resolveOption(final String opt) {
665675
if (actual != null) {
666676
for (final Option option : options) {
667677
if (actual.equals(option.getOpt()) || actual.equals(option.getLongOpt())) {
668-
if (option.isDeprecated()) {
669-
handleDeprecated(option);
670-
}
671678
return option;
672679
}
673680
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ public void testDeprecatedOption() {
9595
handler.set(null);
9696
cmd.getOptionValue("Nope");
9797
assertNull(handler.get());
98+
handler.set(null);
99+
cmd.getOptionValue(opt);
100+
assertSame(opt, handler.get());
101+
}
102+
103+
@Test
104+
public void testDeprecatedParsedOptionValue() throws ParseException {
105+
final CommandLine.Builder builder = new CommandLine.Builder();
106+
builder.addArg("foo").addArg("bar");
107+
final Option opt = Option.builder().option("T").deprecated().build();
108+
builder.addOption(opt);
109+
final AtomicReference<Option> handler = new AtomicReference<>();
110+
final CommandLine cmd = builder.setDeprecatedHandler(handler::set).build();
111+
cmd.getParsedOptionValue(opt.getOpt());
112+
assertSame(opt, handler.get());
113+
handler.set(null);
114+
cmd.getParsedOptionValue("Nope");
115+
assertNull(handler.get());
116+
handler.set(null);
117+
cmd.getParsedOptionValue(opt);
118+
assertSame(opt, handler.get());
98119
}
99120

100121
@Test

0 commit comments

Comments
 (0)