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