commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [24/50] [abbrv] commons-cli git commit: CLI-265: Optional argument picking up next regular option as its argument. Extend fix to address problems identified by Martin Sandiford who also submitted the extended patch.
Date Thu, 08 Jun 2017 17:36:53 GMT
CLI-265: Optional argument picking up next regular option as its argument. Extend fix to address
problems identified by Martin Sandiford who also submitted the extended patch.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1759745 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-cli/commit/ac2a1c85
Tree: http://git-wip-us.apache.org/repos/asf/commons-cli/tree/ac2a1c85
Diff: http://git-wip-us.apache.org/repos/asf/commons-cli/diff/ac2a1c85

Branch: refs/heads/master
Commit: ac2a1c85616f0140418de9190389fe7b80296c39
Parents: 5764b62
Author: Benedikt Ritter <britter@apache.org>
Authored: Thu Sep 8 05:36:59 2016 +0000
Committer: Benedikt Ritter <britter@apache.org>
Committed: Thu Sep 8 05:36:59 2016 +0000

----------------------------------------------------------------------
 src/changes/changes.xml                         |  2 +-
 .../org/apache/commons/cli/DefaultParser.java   |  7 ++++++-
 .../apache/commons/cli/bug/BugCLI265Test.java   | 21 +++++++++++++++++---
 3 files changed, 25 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-cli/blob/ac2a1c85/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d54c4dc..3739ab5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -23,7 +23,7 @@
   <body>
 
     <release version="1.4" date="tba" description="tba">
-      <action type="fix" dev="britter" issue="CLI-265">
+      <action type="fix" dev="britter" issue="CLI-265" due-to="Martin Sandiford">
         Optional argument picking up next regular option as its argument
       </action>
       <action type="add" dev="britter" issue="CLI-267" due-to="Ricardo Ribeiro">

http://git-wip-us.apache.org/repos/asf/commons-cli/blob/ac2a1c85/src/main/java/org/apache/commons/cli/DefaultParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/cli/DefaultParser.java b/src/main/java/org/apache/commons/cli/DefaultParser.java
index f34173e..2cee432 100644
--- a/src/main/java/org/apache/commons/cli/DefaultParser.java
+++ b/src/main/java/org/apache/commons/cli/DefaultParser.java
@@ -307,7 +307,12 @@ public class DefaultParser implements CommandLineParser
         // remove leading "-" and "=value"
         int pos = token.indexOf("=");
         String optName = pos == -1 ? token.substring(1) : token.substring(1, pos);
-        return options.hasShortOption(optName);
+        if (options.hasShortOption(optName))
+        {
+            return true;
+        }
+        // check for several concatenated short options
+        return optName.length() > 0 && options.hasShortOption(String.valueOf(optName.charAt(0)));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-cli/blob/ac2a1c85/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java b/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
index 78ce239..eb05bb8 100644
--- a/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
@@ -27,6 +27,7 @@ import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -43,10 +44,12 @@ public class BugCLI265Test {
     public void setUp() throws Exception {
         parser = new DefaultParser();
 
-        Option TYPE1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
-        Option LAST = Option.builder("last").hasArg(false).build();
+        Option optionT1 = Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
+        Option optionA = Option.builder("a").hasArg(false).build();
+        Option optionB = Option.builder("b").hasArg(false).build();
+        Option optionLast = Option.builder("last").hasArg(false).build();
 
-        options = new Options().addOption(TYPE1).addOption(LAST);
+        options = new Options().addOption(optionT1).addOption(optionA).addOption(optionB).addOption(optionLast);
     }
 
     @Test
@@ -70,4 +73,16 @@ public class BugCLI265Test {
         assertTrue("Second option has not been detected", commandLine.hasOption("last"));
     }
 
+    @Test
+    public void shouldParseConcatenatedShortOptions() throws Exception {
+        String[] concatenatedShortOptions = new String[] { "-t1", "-ab" };
+
+        final CommandLine commandLine = parser.parse(options, concatenatedShortOptions);
+
+        assertTrue(commandLine.hasOption("t1"));
+        assertNull(commandLine.getOptionValue("t1"));
+        assertTrue(commandLine.hasOption("a"));
+        assertTrue(commandLine.hasOption("b"));
+        assertFalse(commandLine.hasOption("last"));
+    }
 }


Mime
View raw message