This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new c63265b  [CLI-331] Handle reporting of deprecated options when 
parameters are not String type. (#270)
c63265b is described below

commit c63265ba42aea420d476f9c410a3e651416d89f8
Author: Claude Warren <cla...@xenei.com>
AuthorDate: Sat May 11 14:46:55 2024 +0100

    [CLI-331] Handle reporting of deprecated options when parameters are not 
String type. (#270)
    
    * fixes
    
    * added additional testing
    
    * Indent
    
    ---------
    
    Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com>
---
 .../java/org/apache/commons/cli/CommandLine.java    | 15 +++++++++++----
 .../org/apache/commons/cli/CommandLineTest.java     | 21 +++++++++++++++++++++
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java 
b/src/main/java/org/apache/commons/cli/CommandLine.java
index 5b9184c..1b49398 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -405,6 +405,9 @@ public class CommandLine implements Serializable {
         if (option == null) {
             return null;
         }
+        if (option.isDeprecated()) {
+            handleDeprecated(option);
+        }
         final List<String> values = new ArrayList<>();
         for (final Option processedOption : options) {
             if (processedOption.equals(option)) {
@@ -498,6 +501,9 @@ public class CommandLine implements Serializable {
         if (option == null) {
             return get(defaultValue);
         }
+        if (option.isDeprecated()) {
+            handleDeprecated(option);
+        }
         final String res = getOptionValue(option);
         try {
             if (res == null) {
@@ -615,7 +621,11 @@ public class CommandLine implements Serializable {
      * @since 1.5.0
      */
     public boolean hasOption(final Option opt) {
-        return options.contains(opt);
+        boolean result = options.contains(opt);
+        if (result && opt.isDeprecated()) {
+            handleDeprecated(opt);
+        }
+        return result;
     }
 
     /**
@@ -665,9 +675,6 @@ public class CommandLine implements Serializable {
         if (actual != null) {
             for (final Option option : options) {
                 if (actual.equals(option.getOpt()) || 
actual.equals(option.getLongOpt())) {
-                    if (option.isDeprecated()) {
-                        handleDeprecated(option);
-                    }
                     return option;
                 }
             }
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java 
b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index eaa972b..c2de38f 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -95,6 +95,27 @@ public class CommandLineTest {
         handler.set(null);
         cmd.getOptionValue("Nope");
         assertNull(handler.get());
+        handler.set(null);
+        cmd.getOptionValue(opt);
+        assertSame(opt, handler.get());
+    }
+
+    @Test
+    public void testDeprecatedParsedOptionValue() throws ParseException {
+        final CommandLine.Builder builder = new CommandLine.Builder();
+        builder.addArg("foo").addArg("bar");
+        final Option opt = Option.builder().option("T").deprecated().build();
+        builder.addOption(opt);
+        final AtomicReference<Option> handler = new AtomicReference<>();
+        final CommandLine cmd = 
builder.setDeprecatedHandler(handler::set).build();
+        cmd.getParsedOptionValue(opt.getOpt());
+        assertSame(opt, handler.get());
+        handler.set(null);
+        cmd.getParsedOptionValue("Nope");
+        assertNull(handler.get());
+        handler.set(null);
+        cmd.getParsedOptionValue(opt);
+        assertSame(opt, handler.get());
     }
 
     @Test

Reply via email to