commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1759745 - in /commons/proper/cli/trunk/src: changes/changes.xml main/java/org/apache/commons/cli/DefaultParser.java test/java/org/apache/commons/cli/bug/BugCLI265Test.java
Date Thu, 08 Sep 2016 05:36:59 GMT
Author: britter
Date: Thu Sep  8 05:36:59 2016
New Revision: 1759745

URL: http://svn.apache.org/viewvc?rev=1759745&view=rev
Log:
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.

Modified:
    commons/proper/cli/trunk/src/changes/changes.xml
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java

Modified: commons/proper/cli/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/changes/changes.xml?rev=1759745&r1=1759744&r2=1759745&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/changes/changes.xml (original)
+++ commons/proper/cli/trunk/src/changes/changes.xml Thu Sep  8 05:36:59 2016
@@ -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">

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java?rev=1759745&r1=1759744&r2=1759745&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java Thu Sep
 8 05:36:59 2016
@@ -307,7 +307,12 @@ public class DefaultParser implements Co
         // 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)));
     }
 
     /**

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java?rev=1759745&r1=1759744&r2=1759745&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI265Test.java Thu
Sep  8 05:36:59 2016
@@ -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