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