You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/07/28 10:27:47 UTC

svn commit: r680287 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/PosixParser.java test/org/apache/commons/cli/PosixParserTest.java

Author: ebourg
Date: Mon Jul 28 01:27:46 2008
New Revision: 680287

URL: http://svn.apache.org/viewvc?rev=680287&view=rev
Log:
Fixed the handling of unrecognized options starting with '-' by PosixParser (CLI-164)

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=680287&r1=680286&r2=680287&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 Mon Jul 28 01:27:46 2008
@@ -125,13 +125,11 @@
             // single hyphen
             else if ("-".equals(token))
             {
-                processSingleHyphen(token);
+                tokens.add(token);
             }
             else if (token.startsWith("-"))
             {
-                int tokenLength = token.length();
-
-                if (tokenLength == 2)
+                if (token.length() == 2)
                 {
                     processOptionToken(token, stopAtNonOption);
                 }
@@ -145,16 +143,13 @@
                     burstToken(token, stopAtNonOption);
                 }
             }
+            else if (stopAtNonOption)
+            {
+                process(token);
+            }
             else
             {
-                if (stopAtNonOption)
-                {
-                    process(token);
-                }
-                else
-                {
-                    tokens.add(token);
-                }
+                tokens.add(token);
             }
 
             gobble(iter);
@@ -217,17 +212,6 @@
     }
 
     /**
-     * If it is a hyphen then add the hyphen directly to
-     * the processed tokens list.
-     *
-     * @param hyphen The hyphen token
-     */
-    private void processSingleHyphen(String hyphen)
-    {
-        tokens.add(hyphen);
-    }
-
-    /**
      * <p>If an {@link Option} exists for <code>token</code> then
      * set the current option and add the token to the processed 
      * list.</p>
@@ -242,14 +226,15 @@
      */
     private void processOptionToken(String token, boolean stopAtNonOption)
     {
-        if (this.options.hasOption(token))
+        if (options.hasOption(token))
         {
-            currentOption = this.options.getOption(token);
+            currentOption = options.getOption(token);
             tokens.add(token);
         }
         else if (stopAtNonOption)
         {
             eatTheRest = true;
+            tokens.add(token);
         }
     }
 

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=680287&r1=680286&r2=680287&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 Mon Jul 28 01:27:46 2008
@@ -130,6 +130,17 @@
         assertTrue( "Confirm  2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
     }
 
+    public void testStop2() throws Exception
+    {
+        String[] args = new String[]{"-z",
+                                     "-a",
+                                     "-btoast"};
+
+        CommandLine cl = parser.parse(options, args, true);
+        assertFalse("Confirm -a is not set", cl.hasOption("a"));
+        assertTrue("Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
+    }
+
     public void testStopBursting() throws Exception
     {
         String[] args = new String[] { "-azc" };