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