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