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