31b554
commit 77218790904f40395304669f5d79740f459c0a90 (HEAD -> cli-253, origin/cli-253)
31b554
Author:     Michal Srb <msrb@redhat.com>
31b554
AuthorDate: Mon Jun 22 15:01:30 2015 +0200
31b554
Commit:     Michal Srb <msrb@redhat.com>
31b554
CommitDate: Mon Jun 22 15:04:05 2015 +0200
31b554
31b554
    [CLI-253] Prevent "Unrecognized option: --null" when handling long opts in PosixParser
31b554
31b554
diff --git a/src/main/java/org/apache/commons/cli/Options.java b/src/main/java/org/apache/commons/cli/Options.java
31b554
index 0ee4eea..1c38194 100644
31b554
--- a/src/main/java/org/apache/commons/cli/Options.java
31b554
+++ b/src/main/java/org/apache/commons/cli/Options.java
31b554
@@ -224,6 +224,20 @@ public class Options implements Serializable
31b554
     }
31b554
 
31b554
     /**
31b554
+     * Retrieve the {@link Option} matching the long name specified.
31b554
+     * The leading hyphens in the name are ignored (up to 2).
31b554
+     *
31b554
+     * @param opt long name of the {@link Option}
31b554
+     * @return the option represented by opt
31b554
+     */
31b554
+    Option getLongOption(String opt)
31b554
+    {
31b554
+        opt = Util.stripLeadingHyphens(opt);
31b554
+
31b554
+        return longOpts.get(opt);
31b554
+    }
31b554
+
31b554
+    /**
31b554
      * Returns the options with a long name starting with the name specified.
31b554
      * 
31b554
      * @param opt the partial name of the option
31b554
diff --git a/src/main/java/org/apache/commons/cli/PosixParser.java b/src/main/java/org/apache/commons/cli/PosixParser.java
31b554
index c13a65e..14d2936 100644
31b554
--- a/src/main/java/org/apache/commons/cli/PosixParser.java
31b554
+++ b/src/main/java/org/apache/commons/cli/PosixParser.java
31b554
@@ -131,7 +131,7 @@ public class PosixParser extends Parser
31b554
                 }
31b554
                 else
31b554
                 {
31b554
-                    currentOption = options.getOption(matchingOpts.get(0));
31b554
+                    currentOption = options.getLongOption(matchingOpts.get(0));
31b554
                     
31b554
                     tokens.add("--" + currentOption.getLongOpt());
31b554
                     if (pos != -1)
31b554
diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java b/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java
31b554
new file mode 100644
31b554
index 0000000..e37b7bc
31b554
--- /dev/null
31b554
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI253Test.java
31b554
@@ -0,0 +1,44 @@
31b554
+/*
31b554
+ * Licensed to the Apache Software Foundation (ASF) under one or more
31b554
+ * contributor license agreements.  See the NOTICE file distributed with
31b554
+ * this work for additional information regarding copyright ownership.
31b554
+ * The ASF licenses this file to You under the Apache License, Version 2.0
31b554
+ * (the "License"); you may not use this file except in compliance with
31b554
+ * the License.  You may obtain a copy of the License at
31b554
+ *
31b554
+ *      http://www.apache.org/licenses/LICENSE-2.0
31b554
+ *
31b554
+ * Unless required by applicable law or agreed to in writing, software
31b554
+ * distributed under the License is distributed on an "AS IS" BASIS,
31b554
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31b554
+ * See the License for the specific language governing permissions and
31b554
+ * limitations under the License.
31b554
+ */
31b554
+
31b554
+package org.apache.commons.cli.bug;
31b554
+
31b554
+import static org.junit.Assert.assertTrue;
31b554
+
31b554
+import org.apache.commons.cli.CommandLine;
31b554
+import org.apache.commons.cli.Option;
31b554
+import org.apache.commons.cli.Options;
31b554
+import org.apache.commons.cli.ParseException;
31b554
+import org.apache.commons.cli.PosixParser;
31b554
+import org.junit.Test;
31b554
+
31b554
+@SuppressWarnings("deprecation") // tests some deprecated classes
31b554
+public class BugCLI253Test {
31b554
+
31b554
+    @Test
31b554
+    public void testGroovyUseCase() throws ParseException {
31b554
+        CommandLine cli = new PosixParser().parse(getOptions(), new String[] { "--classpath" });
31b554
+        assertTrue(cli.hasOption("--classpath"));
31b554
+    }
31b554
+
31b554
+    private Options getOptions() {
31b554
+        Options options = new Options();
31b554
+        options.addOption(Option.builder("classpath").build());
31b554
+        options.addOption(Option.builder("cp").longOpt("classpath").build());
31b554
+        return options;
31b554
+    }
31b554
+}