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