commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r680208 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/PosixParser.java test/org/apache/commons/cli/PosixParserTest.java
Date Sun, 27 Jul 2008 23:38:21 GMT
Author: ebourg
Date: Sun Jul 27 16:38:21 2008
New Revision: 680208

URL: http://svn.apache.org/viewvc?rev=680208&view=rev
Log:
Fixed PosixParser to stop bursting tokens when a non option character is found (CLI-163)

Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/PosixParser.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/PosixParser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/PosixParser.java?rev=680208&r1=680207&r2=680208&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/PosixParser.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/PosixParser.java Sun
Jul 27 16:38:21 2008
@@ -14,11 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.cli;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
+import java.util.List;
 
 /**
  * The class PosixParser provides an implementation of the 
@@ -31,7 +33,7 @@
 public class PosixParser extends Parser {
 
     /** holder for flattened tokens */
-    private ArrayList tokens = new ArrayList();
+    private List tokens = new ArrayList();
 
     /** specifies if bursting should continue */
     private boolean eatTheRest;
@@ -43,9 +45,9 @@
     private Options options;
 
     /**
-     * <p>Resets the members to their original state i.e. remove
+     * Resets the members to their original state i.e. remove
      * all of <code>tokens</code> entries, set <code>eatTheRest</code>
-     * to false and set <code>currentOption</code> to null.</p>
+     * to false and set <code>currentOption</code> to null.
      */
     private void init()
     {
@@ -92,21 +94,19 @@
      * when an non option is found.
      * @return The flattened <code>arguments</code> String array.
      */
-    protected String[] flatten(Options options, String[] arguments, 
-                               boolean stopAtNonOption)
+    protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
     {
         init();
         this.options = options;
 
         // an iterator for the command line tokens
         Iterator iter = Arrays.asList(arguments).iterator();
-        String token;
 
         // process each command line token
         while (iter.hasNext())
         {
             // get the next command line token
-            token = (String) iter.next();
+            String token = (String) iter.next();
 
             // handle SPECIAL TOKEN
             if (token.startsWith("--"))
@@ -114,8 +114,7 @@
                 if (token.indexOf('=') != -1)
                 {
                     tokens.add(token.substring(0, token.indexOf('=')));
-                    tokens.add(token.substring(token.indexOf('=') + 1, 
-                                               token.length()));
+                    tokens.add(token.substring(token.indexOf('=') + 1, token.length()));
                 }
                 else
                 {
@@ -136,8 +135,9 @@
                 {
                     processOptionToken(token, stopAtNonOption);
                 }
-                else if (options.hasOption(token)) {
-                	tokens.add(token);
+                else if (options.hasOption(token))
+                {
+                    tokens.add(token);
                 }
                 // requires bursting
                 else
@@ -164,7 +164,7 @@
     }
 
     /**
-     * <p>Adds the remaining tokens to the processed tokens list.</p>
+     * Adds the remaining tokens to the processed tokens list.
      *
      * @param iter An iterator over the remaining tokens
      */
@@ -183,8 +183,10 @@
      * <p>If there is a current option and it can have an argument
      * value then add the token to the processed tokens list and 
      * set the current option to null.</p>
+     *
      * <p>If there is a current option and it can have argument
      * values then add the token to the processed tokens list.</p>
+     *
      * <p>If there is not a current option add the special token
      * "<b>--</b>" and the current <code>value</code> to the processed
      * tokens list.  The add all the remaining <code>argument</code>
@@ -194,7 +196,7 @@
      */
     private void process(String value)
     {
-        if ((currentOption != null) && currentOption.hasArg())
+        if (currentOption != null && currentOption.hasArg())
         {
             if (currentOption.hasArg())
             {
@@ -215,8 +217,8 @@
     }
 
     /**
-     * <p>If it is a hyphen then add the hyphen directly to
-     * the processed tokens list.</p>
+     * If it is a hyphen then add the hyphen directly to
+     * the processed tokens list.
      *
      * @param hyphen The hyphen token
      */
@@ -229,6 +231,7 @@
      * <p>If an {@link Option} exists for <code>token</code> then
      * set the current option and add the token to the processed 
      * list.</p>
+     *
      * <p>If an {@link Option} does not exist and <code>stopAtNonOption</code>
      * is set then ignore the current token and add the remaining tokens
      * to the processed tokens list directly.</p>
@@ -278,14 +281,11 @@
      */
     protected void burstToken(String token, boolean stopAtNonOption)
     {
-        int tokenLength = token.length();
-
-        for (int i = 1; i < tokenLength; i++)
+        for (int i = 1; i < token.length(); i++)
         {
             String ch = String.valueOf(token.charAt(i));
-            boolean hasOption = options.hasOption(ch);
 
-            if (hasOption)
+            if (options.hasOption(ch))
             {
                 tokens.add("-" + ch);
                 currentOption = options.getOption(ch);
@@ -300,6 +300,7 @@
             else if (stopAtNonOption)
             {
                 process(token.substring(i));
+                break;
             }
             else
             {

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java?rev=680208&r1=680207&r2=680208&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/PosixParserTest.java
Sun Jul 27 16:38:21 2008
@@ -130,6 +130,18 @@
         assertTrue( "Confirm  2 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 2);
     }
 
+    public void testStopBursting() throws Exception
+    {
+        String[] args = new String[] { "-azc" };
+
+        CommandLine cl = parser.parse(options, args, true);
+        assertTrue( "Confirm -a is set", cl.hasOption("a") );
+        assertFalse( "Confirm -c is not set", cl.hasOption("c") );
+
+        assertTrue( "Confirm  1 extra arg: " + cl.getArgList().size(), cl.getArgList().size()
== 1);
+        assertTrue(cl.getArgList().contains("zc"));
+    }
+
     public void testMultiple() throws Exception
     {
         String[] args = new String[] { "-c",
@@ -155,8 +167,7 @@
                                        "foobar",
                                        "--bfile", "toast" };
 
-        CommandLine cl = parser.parse(options,args,
-                                        true);
+        CommandLine cl = parser.parse(options,args, true);
         assertTrue( "Confirm -c is set", cl.hasOption("c") );
         assertTrue( "Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size()
== 3);
 



Mime
View raw message